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

I am new to perl. Could you please guide me how to achieve that.Thanks in advance.

Replies are listed 'Best First'.
Re^5: Converting Excel to Hash
by Corion (Patriarch) on Jan 03, 2017 at 15:31 UTC

    See perlintro and perldsc maybe.

    The idea is to initialize $data{ $master_key } with an empty array for each id and then to push every found element onto that array:

    ... $data{$master_key} ||= []; # if we haven't seen $master_key yet, creat +e a fresh array my %this_set; for my $col(1 .. $eSheet->{MaxCol}) { # Some logging for debugging if( !defined $eSheet->{Cells}[$row][$col]) { print "Row $row: skipping empty column $col\n"; next; }; my $key= $eSheet->{Cells}[0][$col]->Value; my $value = $eSheet->{Cells}[$row][$col]->Value; $this_set{$key}=$value; } } # Store our found set for that ID: push @{ $data{ $master_key }}, \%this_set;
      use Spreadsheet::ParseExcel; use Data::Dumper; $filename="Book2.xls"; $e=new Spreadsheet::ParseExcel; $eBook=$e->Parse($filename); $sheets = $eBook->{SheetCount}; ($eSheet, $sheetName); foreach $sheet (0 .. $sheets - 1) { $eSheet = $eBook->{Worksheet}[$sheet]; $sheetName = $eSheet->{Name}; print "Worksheet $sheet: $sheetName\n"; %set =(); %data =(); foreach $row( 1 .. $eSheet->{MaxRow} ) { $master_key= ($eSheet->{Cells}[$row][0]->Value); foreach $col(1 .. $eSheet->{MaxCol}) { next unless defined $eSheet->{Cells}[$row][$col]; my $key=$eSheet->{Cells}[0][$col]->Value; my $val=($eSheet->{Cells}[$row][$col]->Value); $set{$key}=$val; } } push @{ $data{$master_key}}, \%set; print Dumper %data; }
      $var1='2'; $var2=[{ clz=>iir, degreee=>b.tech, name=>ravi, } ];

      only last row is coming

      .

        The line push @{ $data{$master_key}}, \%set;
        should be inside the foreach $row ( 1 .. $eSheet->{MaxRow} ) loop.

        If you indent your code sensibly you will see the error more easily.

        poj
Re^5: Converting Excel to Hash
by poj (Abbot) on Jan 03, 2017 at 16:19 UTC

    Where the spreadsheet is missing a master-key are you assuming the previous value ?

    ID NAME Degree Collage 1 Teja B.Tech NIT M.Tech IIT(the problem is in converting this line) 2 Ravi B.tech MIT
    poj

      yes.Could you help me in this

        Try

        #!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;
        update : clear @datarow for header line.
        poj