Trying to take out all ATOM from pdb file

别等时光非礼了梦想. 提交于 2020-01-03 04:54:28

问题


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";

回答1:


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";


来源:https://stackoverflow.com/questions/47723084/trying-to-take-out-all-atom-from-pdb-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!