【问题标题】:Trying to take out all ATOM from pdb file试图从 pdb 文件中取出所有 ATOM
【发布时间】:2018-05-23 05:08:56
【问题描述】:
您好,我正在尝试从 pdb 文件中取出所有以 ATOM 开头的行。出于某种原因,我遇到了麻烦。我的代码是:
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";
【问题讨论】:
标签:
regex
bash
shell
perl
pdb-files
【解决方案1】:
线
@newlines = $lines;
将$lines 重新分配给数组@newlines,从而在while 循环的每次迭代中覆盖它。
你宁愿追加每个$lines到@newlines,所以
push @newlines, $lines;
会起作用的。
旁注:变量名$lines 应该是$line(只是为了便于阅读),因为它只是一行,而不是多行。
您可以在循环后使用@newlines 中的项目数,而不是显式计算附加到@newlines 的项目(使用$atomcount++;):
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";