我有一个这样的 CSV 文件:
name,email,salary
a,b@b.com,1000
d,e@e.com,2000
现在,我需要将其转换为 Perl 中的哈希映射数组,所以当我执行以下操作时:
table[1]{"email"}
它返回 e@e.com。
我写的代码是:
open(DATA, "<$file") or die "Cannot open the file\n";
my @table;
#fetch header line
$line = <DATA>;
my @header = split(',',$line);
#fetch data tuples
while($line = <DATA>)
{
my %map;
my @row = split(',',$line);
for($index = 0; $index <= $#header; $index++)
{
$map{"$header[$index]"} = $row[$index];
}
push(@table, %map);
}
close(DATA);
但我没有得到想要的结果..你能帮忙吗?提前致谢...