我的任务是从以下数据文件中计算平均值,标题为Lab1_table.txt
:
retrovirus genome gag pol env
HIV-1 9181 1503 3006 2571
FIV 9474 1353 2993 2571
KoRV 8431 1566 3384 1980
GaLV 8088 1563 3498 2058
PERV 8072 1560 3621 1532
我必须编写一个脚本来打开并读取这个文件,通过将内容拆分为一个数组来读取每一行,然后计算数值的平均值(genome
, gag
, pol
, env
),然后将每个文件的平均值写入一个新文件上述列。
我一直在尽力弄清楚如何不考虑第一行或第一列,但是每次我尝试在命令行上执行时,我都会不断出现“显式包名称”错误。
Global symbol @average requires explicit package name at line 23.
Global symbol @average requires explicit package name at line 29.
Execution aborted due to compilation errors.
我知道这涉及@
and $
,但即使知道我无法更改错误。
这是我的代码,但我强调我是上周刚开始的初学者:
#!/usr/bin/perl -w
use strict;
my $infile = "Lab1_table.txt"; # This is the file path
open INFILE, $infile or die "Can't open $infile: $!";
my $count = 0;
my $average = ();
while (<INFILE>) {
chomp;
my @columns = split /\t/;
$count++;
if ( $count == 1 ) {
$average = @columns;
}
else {
for( my $i = 1; $i < scalar $average; $i++ ) {
$average[$i] += $columns[$i];
}
}
}
for( my $i = 1; $i < scalar $average; $i++ ) {
print $average[$i]/$count, "\n";
}
如果有任何见解,我将不胜感激,我也非常感谢通过列出您在每个步骤中所做的事情来让我知道 - 如果合适的话。我想学习,如果我能够阅读某人的流程,这对我来说会更有意义。