我很努力地爲幾個任務編寫一個Perl程序。自從我是初學者並且想要了解我的錯誤之後,我已經非常努力地檢查所有錯誤,但是我失敗了。希望我對迄今爲止的任務和缺陷計劃的描述不會混淆。定義哈希值和密鑰並使用多個不同的文件
在我當前的目錄中,我有一個可變數量的「.txt。」文件。 (我可以有4,5,8或任意數量的文件,但我不認爲我會得到更多的17個文件)。「.txt」文件的格式是相同的。有六列,用白色空格分隔。我只關心這些文件中的兩列:第二列是珊瑚礁regionID(由字母和數字組成),第五列是p值。每個文件中的行數是未確定的。我需要做的是在所有.txt文件中查找所有常見regionID,並將這些常見區域打印到outfile中。但是,在印刷之前,我必須對它們進行分類。
以下是我的程序到目前爲止,但我收到了錯誤消息,我已經包括在程序後。因此,我對變量的定義是主要問題。我非常感謝編寫該程序的任何建議,並感謝您對像我這樣的初學者的耐心。
更新:我已經按照建議聲明瞭變量。查看我的程序後,出現兩個語法錯誤。
syntax error at oreg.pl line 19, near "$hash{"
syntax error at oreg.pl line 23, near "}"
Execution of oreg.pl aborted due to compilation errors.
這裏是編輯程序的摘錄,其中包括所述錯誤的位置。
#!/user/bin/perl
use strict;
use warnings;
# Trying to read files in @txtfiles for reading into hash
foreach my $file (@txtfiles) {
open(FH,"<$file") or die "Can't open $file\n";
while(chomp(my $line = <FH>)){
$line =~ s/^\s+//;
my @IDp = split(/\s+/, $line); # removing whitespace
my $i = 0;
# trying to define values and keys in terms of array elements in IDp
my $value = my $hash{$IDp[$i][1]};
$value .= "$IDp[$i][4]"; # confused here at format to append p-values
$i++;
}
}
close(FH);
這些都是過去的錯誤:
Global symbol "$file" requires explicit package name at oreg.pl line 13.
Global symbol "$line" requires explicit package name at oreg.pl line 16.
#[And many more just like that...]
Execution of oreg.pl aborted due to compilation errors.
感謝您的迅速回復。我很抱歉,但我對「聲明」的含義感到困惑。 – user2174373 2013-03-16 20:37:09
'use strict'必須聲明變量,例如與'我的$變量'之前使用它們。 – rjh 2013-03-16 21:06:43
@ user2174373,正確使用'my',如我的答案所示。 – ikegami 2013-03-16 21:37:16