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

Expected Output

{ '1'=> { 'clz=>'nit' 'degree'=>'ph.d', 'name'=>'teja' }, '1'=> { 'clz=>'iit' 'degree'=>'M.tech', 'name'=>'teja' },####This block not coming in output '2'=>{ 'clz=>'mit' 'degree'=>'ravi', 'name'=>'B.Tech' } };

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

    A hash cannot have multiple values for the same key. You will need to rethink your data structure.

    Consider storing array references as values instead of hash references as values:

    { '1'=> [{ 'clz=>'nit' 'degree'=>'ph.d', 'name'=>'teja' }, { 'clz=>'iit' 'degree'=>'M.tech', 'name'=>'teja' }],####This block not coming in output '2'=>[{ 'clz=>'mit' 'degree'=>'ravi', 'name'=>'B.Tech' }] };

    This change will also need changes in your code on how you fill the data structure and how you output your data structure.

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

        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;

        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