试图从 pdb 文件中取出所有 ATOM [英] Trying to take out all ATOM from pdb file

查看:87
本文介绍了试图从 pdb 文件中取出所有 ATOM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试从 pdb 文件中取出所有以 ATOM 开头的行.出于某种原因,我遇到了麻烦.我的代码是:

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

推荐答案

 @newlines = $lines;

$lines 重新分配给数组 @newlines,从而在 while 循环的每次迭代中覆盖它.您更愿意将每个 $lines append 附加到 @newlines,所以

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;

会起作用.

旁注:变量名 $lines 应该是 $line(只是为了便于阅读)因为它只是 一行,而不是 多行.

Sidenote: The variable name $lines should be $line (just for readability) because it's just one line, not multiple lines.

您可以只使用 @newlines 中的项目数,而不是显式计算附加到 @newlines 的项目(使用 $atomcount++;)> 循环后:

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

这篇关于试图从 pdb 文件中取出所有 ATOM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆