我的表结构是:
Name Address
Deepti 1325
Cizwan 324
rikita 567
我必须制作像[{Name->deepti,Address->1325},{Name->Cizwan,Address->324},{Name->Rikita,Address->567}]
该表位于excel数据中,因此存在多行。 提前致谢。我尝试了一些但无法继续的事情。
for my $row_num (2..($max_rows))
{
if(exists $workbook->{'cell'}[1][1])
{
#insert values in the hash
$id1 = $workbook->{'cell'}[1][1];
my $val1 = $workbook->{'cell'}[1][$row_num];
#push values in hash
push (@{$hash1{$id1}},$val1);
}
if(exists $workbook->{'cell'}[2][1])
{
#insert values in the hash
$id2 = $workbook->{'cell'}[2][1];
my $val1 = $workbook->{'cell'}[2][$row_num];
#push values in hash
push (@{$hash2{$id2}},$val1);
}
}
print Dumper \%hash1;
print Dumper \%hash2;
答案 0 :(得分:0)
所以对于你的结构,你看起来像一堆匿名哈希就是你想要的。
这比你想象的要简单:
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
my @rows;
chomp ( my @header = split ' ', <DATA> );
while ( <DATA> ) {
chomp;
my %row;
@row{@header} = split;
push @rows, \%row;
}
print Dumper \@rows;
__DATA__
Name Address
Deepti 1325
Cizwan 324
rikita 567
This outputs:
$VAR1 = [
{
'Address' => '1325',
'Name' => 'Deepti'
},
{
'Name' => 'Cizwan',
'Address' => '324'
},
{
'Address' => '567',
'Name' => 'rikita'
}
];
但要将其纳入您的代码 - 您可能希望:
push ( @rows, { Name => $id2, Address => $val } );
这是有效的,因为在{}
中放置这样的值会导致它返回一个匿名哈希,然后您可以将其插入到数组中。