我写了一个脚本来提取两列;第一个是用户在参数中输入的列,第二个列是第一个列的下一列;现在我必须单独添加列。
请注意,该文件是CSV文件,其字符串为coloumns的标题
#!/usr/bin/perl
use warnings;
use strict;
my $file = $ARGV[0];
my $coloumn_number = $ARGV[1];
my $addition = 0;
my $sum = 0;
open(my $filedata, '<', $file) or die "Can't read file '$file'\n";
while (my $line = <$filedata>) {
chomp $line;
my @fields = split(",", $line);
print "$fields[$coloumn_number],";
print "$fields[$coloumn_number+1]\n";
$sum += $fields[$coloumn_number];
$addition += $fields[coloumn_number+1];
}
print "$sum";
print "$addition";
当我运行此脚本时,我收到错误
-bash-4.1$ ./script1.pl /diff2.csv 2
Bareword "coloumn_number" not allowed while "strict subs" in use at ./script1.pl line 17.
Execution of ./script1.pl aborted due to compilation errors.
当我在没有最后5行添加部分的情况下运行脚本时,它可以正常工作。
答案 0 :(得分:0)
您错过了$
中的$addition += $fields[coloumn_number+1];
。应为$addition += $fields[$coloumn_number+1];