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
re-assigns
$lines
to the array@newlines
and thus overwrites it with every iteration of thewhile
loop. You rather want to append every$lines
to@newlines
, sowill 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: