很抱歉,如果我的问题太明显了,我是perl的新人。
我的代码如下:
open (FILE1, "$ARG[0]") or die
@lines1;
$i=1;
while (<FILE>) {
chomp;
push (@lines1, $_);
my @{columns$1}= split (/\s+/, $lines1[$i]);
$i++;
}
它出错了
Can´t declare array dereference at the line my @{columns$1}= split (/\s+/, $lines1[$i]);
我想创建columns1,columns2,columns3 ......并且每一个都会有相应行的列(第1行的第1列,第2行的第2列,依此类推......)
因为在我尝试这样做之前(下面)并且每次分割线但是它覆盖了@ columns1数组,所以只保存了最后一行,最后我得到了第10行的值(因为它从0开始计数)
for my $i (0..9) {
@columns1 = split (/\s+/, $lines1[$i]);
}
答案 0 :(得分:1)
要在其列中拆分表文件,您可以执行以下操作:
#!/usr/bin/perl
#ALWAYS put 'use warnings' and 'use strict' on the beginning of your code. It makes
#your life easier when debugging your code, and save you from having empty variables
#making weird things all over your code, and many other things.
#It is a good practice for "safe Perl coding".
use warnings;
use strict;
my ($file) = @ARGV;
open(my $in, "<$ARGV[0]"); #In your code you used an old filehandle format, FILE1.
#You should use the new format - $file1 as it allows you
#to use any scalar variable as a filehandle.
my @column1;
while(<$in>) {
chomp;
#Here comes the splitting:
my @table = split(/\s+/);
#if you want to print the first column:
print "$table[0]\n"; #remember that Perl starts to count from 0;
#if you know which columns you want to work with:
push(@column1, $table[0]);
}
即使我擅长先做和先学会修改 - 你的错误方法来学习编码,但你应该采取一段时间来完成Perl的基础知识,正如 @mpapec 所说。学习基础知识可以在处理像你这样的问题时节省大量的时间和精力。