Hello I am trying to take out all the lines that start with ATOM from a pdb file. For some reason, I am having trouble. My code is:
open (FILE, $ARGV[0])
or die "Could not open file\n";
my @newlines;
my $atomcount = 0;
while ( my $lines = <FILE> ) {
if ($lines =~ m/^ATOM.*/) {
@newlines = $lines;
$atomcount++;
}
}
print "@newlines\n";
print "$atomcount\n";
The line
@newlines = $lines;
re-assigns $lines
to the array @newlines
and thus overwrites it with every iteration of the while
loop.
You rather want to append every $lines
to @newlines
, so
push @newlines, $lines;
will work.
Sidenote: The variable name $lines
should be $line
(just for readability) because it's just one line, not multiple lines.
Instead of explicitly counting the items appended to @newlines
(with $atomcount++;
) you can just use the number of items in @newlines
after the loop:
my @newlines;
while ( my $line = <FILE> ) {
if ($line =~ m/^ATOM.*/) {
push @newlines, $line;
}
}
my $atomcount = @newlines; # in scalar context this is the number of items in @newlines
print "@newlines\n";
print "$atomcount\n";