in reply to Re^6: Converting Excel to Hash
in thread Converting Excel to Hash
Try
update : clear @datarow for header line.#!perl use strict; use Spreadsheet::ParseExcel; use Data::Dump 'dd'; my $filename = "Book2.xls"; my $e = new Spreadsheet::ParseExcel; my $eBook = $e->Parse($filename); my $eSheet = $eBook->{Worksheet}[0]; my $maxrow = $eSheet->{MaxRow}; my $maxcol = $eSheet->{MaxCol}; print "maxrow=$maxrow maxcol=$maxcol\n"; my %data = (); my @header = (); my @datarow = (); for my $row (0 .. $maxrow) { my %this_set=(); my $master_key; for my $col (0 .. $maxcol){ my $cell = $eSheet->{Cells}[$row][$col]; if (defined $cell){ $datarow[$col] = $cell->Value unless ($cell->Value eq ''); } else { next; } # header row if ($row == 0){ $header[$col] = $datarow[$col]; $datarow[$col] = ''; } else { if ($col == 0){ $master_key = $datarow[$col]; } else { my $key = $header[$col]; my $value = $datarow[$col]; $this_set{$key} = $value; } } } #print join ",",@datarow,"\n"; # Store our found set for that ID: if ($row > 0){ push @{ $data{ $master_key }}, \%this_set; } } dd \%data;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^8: Converting Excel to Hash
by ravi179 (Novice) on Jan 04, 2017 at 11:26 UTC | |
by hippo (Archbishop) on Jan 04, 2017 at 11:34 UTC | |
by ravi179 (Novice) on Jan 04, 2017 at 11:59 UTC | |
by Corion (Patriarch) on Jan 04, 2017 at 12:03 UTC | |
by ravi179 (Novice) on Jan 04, 2017 at 12:19 UTC | |
| |
by poj (Abbot) on Jan 04, 2017 at 11:59 UTC | |
by ravi179 (Novice) on Jan 05, 2017 at 04:52 UTC | |
by poj (Abbot) on Jan 05, 2017 at 08:23 UTC | |
by ravi179 (Novice) on Jan 05, 2017 at 08:48 UTC |