【发布时间】:2017-10-01 14:27:04
【问题描述】:
我正在尝试读取 CVS 文件以获取其中的第 2 列和第 3 列。我在 while 循环之外声明了空数组 @x_ax 和 @dat。我使用@x_ax 存储第二列和@dat 存储CSV 文件的第三列。
@x_ax 和 @dat 的内容仅在 while 循环内部打印,而不在外部打印。
下面是我的代码和输出。
#!/usr/bin/perl
use strict;
my @arr=();
my @x_ax=();
my @dat=();
open(FH,"new1.csv") || die "Not able to open new1.csv file $!\n";
while( my $line=<FH>)
{
if ($line =~ m/Time/)
{
exit 0;
}
else
{
@arr=split(/,/,$line);
#print "@arr[1]\n";
push(@x_ax,$arr[1]);
print "@x_ax \n";
push(@dat,$arr[2]);
}
}
print @x_ax;
print "$#x_ax and $#dat \n";
[root@localhost perl_practice]# perl test.pl
AX_1
AX_1 SAL
AX_1 SAL BAS
AX_1 SAL BAS OPT
AX_1 SAL BAS OPT LES
AX_1 SAL BAS OPT LES MSS
以下是我的 CSV 文件内容
17:00:01,AX_1,0,0,0,0,0
17:00:01,SAL,0,0,0,0,0
17:00:01,BAS,0,0,0,0,0
16:55:02,OPT,0,0,0,0,0
17:00:01,LES,0,0,0,0,0
16:55:02,MSS,0,0,0,0,0
Time,info,dat1,dat2,dat3,dat4,dat5
如何在while循环外访问@x_ax和@dat?
【问题讨论】: