[英]How do I create a hash of hashes from Spreadsheet::ParseExcel data?
I am working with the Spreadsheet::ParseExcel module. 我正在使用Spreadsheet :: ParseExcel模块。 I want to create a hash of hash with the Excel data:
我想用Excel数据创建哈希哈希:
{
'P343453' => {
'Date' => 03022011,
'Method' => 'No',
'Time' => 1440,
},
'P343763' => {
'Date' => 03022011,
'Method' => 'YES',
'Time' => '1745',
}
}
What I have now is something like this: 我现在拥有的是这样的:
{
'P343453' => [
'03022011',
'No',
'1440',
],
'P343763' => [
'03022011',
'YES',
'1745',
],
}
Here is my code: 这是我的代码:
my @worksheets = $workbook->worksheets();
warn "More than 1 worksheet found\n" if @worksheets > 1;
# Only work with first page.
my $worksheet = $worksheets[0];
my ( $row_min, $row_max ) = $worksheet->row_range();
my ( $col_min, $col_max ) = $worksheet->col_range();
my @required_col = (1 .. $col_max);
my @value;
##to get the headers of the excel file;
for my $row ( 0 .. 0 ) {
@value = map {
my $cell = $worksheet->get_cell($row, $_);
$cell ? $cell->value() : '';
} @required_col;
}
# Skip header row
my $sample_details = {};
for my $row (1 .. $row_max ) {
my @data = map {
my $cell = $worksheet->get_cell($row, $_);
$cell ? $cell->value() : '';
} @required_col;
$sample_details->{$worksheet->get_cell($row,0)->value()} = \@data
if defined $worksheet->get_cell($row, 0)->value();
}
How should I modify the code to assign the keys to the hashrefs of the hash from the array @value
? 我应该如何修改代码的键分配给该数组中的散列的hashrefs
@value
?
# Skip header row
my $sample_details = {};
for my $row (1 .. $row_max ) {
my @data = map {
my $cell = $worksheet->get_cell($row, $_);
$cell ? $cell->value() : '';
} @required_col;
foreach my $col (@values) {
$sample_details->{$worksheet->get_cell($row,0)->value()}{$col} = shift @data if defined $worksheet->get_cell($row, 0)->value();
}
}
Replace the \\@data
in the last line with this: 用这个替换最后一行中的
\\@data
:
{ map { ($value[$_] => $data[$_]) } (0..$#value) }
This will make a key/value pair from each list value using the corresponding header as key. 这将使用相应的标题作为键从每个列表值创建一个键/值对。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.