Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays PERL:拆分和读取/访问一行中的元素_Arrays_Perl_Split - Fatal编程技术网

Arrays PERL:拆分和读取/访问一行中的元素

Arrays PERL:拆分和读取/访问一行中的元素,arrays,perl,split,Arrays,Perl,Split,我试图构建一个脚本来读取文件中的行,通过制表符分隔符拆分它们(最终添加两列等),但是在SPLIT命令之后for(..似乎没有将值读取到数组@netcolumns中。输出文件有一行(拆分之前),然后是一行制表符..但为空 #!/usr/bin/perl # use strict; use warnings; use diagnostics; # input and output files my $file = "testfile4.txt"; # t

我试图构建一个脚本来读取文件中的行,通过制表符分隔符拆分它们(最终添加两列等),但是在SPLIT命令之后for(..似乎没有将值读取到数组@netcolumns中。输出文件有一行(拆分之前),然后是一行制表符..但为空

#!/usr/bin/perl
# 
use strict;
use warnings;
use diagnostics;

# input and output files

my $file = "testfile4.txt";                     # tab seperated data
my $binout = $file . ".binned.txt";

# open filehandles

open IN, "<$file" or die "cannot open $file: $!\n";
open BOUT, ">$binout" or die "cannot open $binout: $!\n";

# some other variables
my @netcolumns=();                             # declare this

# the first line
my $firstLine = <IN>;
print ("$firstLine \n");
print BOUT ("$firstLine \n");

my $netcolumns = split /\t/, $firstLine;    # tab seperated data
    for (my $j = 0; $j < 7; $j ++) {        # print the 7 column headers from the first line of $input
        print BOUT "$netcolumns[$j]\t";
    }
print BOUT "\n";
print ("$netcolumns[0] \n");

close IN;
close BOUT;
!/usr/bin/perl
# 
严格使用;
使用警告;
使用诊断;
#输入和输出文件
my$file=“testfile4.txt”#制表符分隔的数据
my$binout=$file.“.binned.txt”;
#打开文件句柄
在“$binout”中打开或在“die”中无法打开$binout:$!\n”;
#其他一些变量
我的@netcolumns=();#声明此
#第一行
我的$firstLine=;
打印(“$firstLine\n”);
打印布特(“$firstLine\n”);
my$netcolumns=split/\t/,$firstLine;#制表符分隔的数据
对于(my$j=0;$j<7;$j++){#打印$input第一行的7个列标题
打印“$netcolumns[$j]\t”;
}
打印关于“\n”;
打印(“$netcolumns[0]\n”);
接近;
近距离回合;
屏幕输出显示了文件的第一行,因此$firstLine从文件中获取了行。我得到的警告是熟悉的“在@netcolumns中使用未初始化值进行连接(.)或在bin_data_4.pl第26行第1行(#1)(W未初始化)处使用字符串”

数据文件如下所示:

frame.time_epoch frame.number frame.len ip.src sctp.srcport ip.dst sctp.dstport 1376065574.000054 1514172.17.78.2638733172.16.189.1803868
1376065574.000054 2 178 172.17.78.26 38733 172.16.189.180 3868
1376065574.000095 362 172.16.189.180 3868 172.17.78.26 38733
1376065574.000499410 172.17.78.26 38733 172.16.189.180 3868
1376065574.000536 5 62 172.16.189.180 3868 172.17.78.26 38733
1376065574.000754 6 410 172.17.78.26 38733 172.16.189.180 3868
1376065574.001108 7 410 172.17.78.26 38733 172.16.189.180 3868
1376065574.0011498 62 172.16.189.180 3868 172.17.78.26 38733
1376065574.001251 9 1514 172.17.78.26 38733 172.16.189.180 3868
1376065574.001251 10178 172.17.78.26 38733 172.16.189.180 3868


我似乎无法在第30行中找到错误,但我认为这是一个明显的错误。感谢您提供的任何帮助。

您有
my$netcolumns=split/\t/,$firstLine;
我想您想要
@netcolumns=split/\t/,$firstLine;
马特是对的。当您在标量上下文中分配
split
的结果时,您得到的是n拆分数据中的值数,大致与编写
my$n=@array;
时得到数组中分配给
$n
的元素数的方式相同。谢谢。Matt是正确的,这很有帮助-我只需要花更多的时间来处理这个问题。