【发布时间】:2014-03-28 21:39:54
【问题描述】:
我正在尝试将一个大矩阵(一个制表符分隔的文件,每列中的元素数量不同)转换为数组的散列。在第一步中,我成功加载文件并使用 Test::CSV 将数据的列转换为列表,但在此过程中,我注意到每个列表的长度是对应于较大列的元素数,即对于那些元素较少的列,存在一个空格。到目前为止,这是我的代码:
#!/usr/bin/perl
use warnings;
use strict;
use Text::CSV;
my $csv = Text::CSV->new({
sep_char => "\t",
});
open( LIST, "<", "testfile" ) or die "No esta el archivo\n";
while (<LIST>) {
if ($csv->parse($_)) {
my @columns = $csv->fields();
print "$columns[0]\t$columns[1]\t$columns[2]","\n";
} else {
my $err = $csv->error_input;
}
}
close(LIST);
输入矩阵有 20 列,4500-8500 行加上一个标题行(理想情况下,我想将其用作哈希中的键)。为简单起见,我构建了一个包含三列的“测试文件”,没有标题和不同数量的元素(与原始输入文件的格式相同)。下面是testfile的内容:
1 1 2
2 2
3 4
5 6
6 7
7 8 8
8 9
这是输出。我认为“使用未初始化的值...”与空格有关。
1 1 2
Use of uninitialized value in concatenation (.) or string at blast.cruce.especies.pl line 16, <LIST> line 3.
2 2
3 4
Use of uninitialized value in concatenation (.) or string at blast.cruce.especies.pl line 16, <LIST> line 5.
5 6
Use of uninitialized value in concatenation (.) or string at blast.cruce.especies.pl line 16, <LIST> line 6.
6 7
7 8 8
Use of uninitialized value in concatenation (.) or string at blast.cruce.especies.pl line 16, <LIST> line 8.
8 9
Use of uninitialized value in concatenation (.) or string at blast.cruce.especies.pl line 16, <LIST> line 9.
Use of uninitialized value in concatenation (.) or string at blast.cruce.especies.pl line 16, <LIST> line 9.
【问题讨论】: