我很努力地为几个任务编写一个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