in reply to Re^12: Converting Excel to Hash
in thread Converting Excel to Hash

You might find the task easier if you split it into 2 steps, first create an array with the missing data filled down and then create the hash. For example

#!perl use strict; use Spreadsheet::ParseExcel; use Data::Dumper; my $filename = "Book2.xls"; my $e = new Spreadsheet::ParseExcel; my $eBook = $e->Parse($filename); my $eSheet = $eBook->{Worksheet}[0]; my ( $row_min, $row_max ) = $eSheet->row_range(); my ( $col_min, $col_max ) = $eSheet->col_range(); # build array filling in blanks my @row_array =(); my @data_array=(); for my $row ($row_min .. $row_max ){ for my $col ($col_min .. $col_max){ my $cell = $eSheet->get_cell( $row, $col ); if ($cell && $cell->value ne ''){ $row_array[$col] = $cell->value; } } $data_array[$row] = [@row_array]; } print Dumper \@data_array; # convert array to hash my %data = (); for my $row ($row_min+1 .. $row_max){ my %set = (); my $master_key = $data_array[$row][$col_min]; for my $col ($col_min+1 .. $col_max){ my $key = $data_array[$row_min][$col]; $set{$key}= $data_array[$row][$col] } push @{ $data{$master_key} }, \%set; } print Dumper \%data;
update : push @data_array,[@row_array] changed
to $data_array[$row] = [@row_array] to allow for $row_min not being 0.
poj