我跑了一個分子動力學模擬,給了我2000個軌跡文件。這些文件的文件名格式爲au25-c2-benzalacetone.001
,最高爲2000.我編寫了一個普通的perl程序,但它沒有讀取2000個文件。如何修改我的代碼以便它讀取每個文件並分別提取每個文件的座標?我需要2,000 coordinates_of_interest.dat
文件。這裏的修補程序文件是au25-c2-benzalacetone
文件。這是我寫的代碼:如何在Perl上正確指定這些變量?
#!/usr/bin/env perl
use Math::VectorReal;
use Math::Trig qw/acos/;
use strict;
use warnings;
my $file1 = $ARGV[0];
my $n1 = $ARGV[1];
my $n2 = $ARGV[2];
my $tinker_file = sprintf "%s.%03d"
my($file1, $n1, $n2) = @ARGV;
foreach $tinker_file (glob "$tinker_file.*") {
print "Filename: $tinker_file\n";
}
my $file2 = "coordinates_of_interest.dat";
my %lines_of_interest = map { $_ => 1 } 18, 25, 26;
{
open(FILE2, '>', $file2) or die "couldn't open the file!";
for(my $i=$n1;$i<=$n2;$i++){
{
open(FILE1, '<', $tinker_file) or die "couldn't open the file!";
{
my $num_lines = keys %lines_of_interest;
while (<FILE1>) {
if ($lines_of_interest{$.}) {
print FILE2;
last unless --$num_lines;
}
}
}
}
}
}
〜
〜
〜
您需要解決幾個語法錯誤(''上$ n1'和'$ n2' my'),'$ i'不與'my',你的'for'未與關閉'}'宣佈,你的第一個'open'沒有正確終止(缺少')'...)。你應該也可以暗示'close()'你的文件句柄。 –
是的。我即將在此處進行編輯。我運行了代碼,它給了我FILE1和FILE2的問題。 –
謝謝@DravSloan –