Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: excel to hash

by Discipulus (Monsignor)
on Sep 13, 2017 at 16:39 UTC ( #1199333=note: print w/replies, xml ) Need Help??


in reply to excel to hash

Hello colox,

you got already two useful answers. What I can add is that Spreadsheet::ParseExcel (the underliyng module as haukex suggested) does not supports .xlsx files. Error checking, as wisely proposed by NetWallah would probably addressed you in rigth direction.

You can process such files using Spreadsheet::XLSX Spreadsheet::ParseXLSX (see below) but abandoning the use of Spreadsheet::ExcelHashTable but I think that get all the content into an hash will not be so difficult.



L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^2: excel to hash
by Tux (Abbot) on Sep 14, 2017 at 06:29 UTC
Re^2: excel to hash
by colox (Novice) on Sep 13, 2017 at 16:43 UTC
    thank you so much for your inputs. i tried to save the spreadsheet into older 2003 XLS & the error is gone. Below is the hash output using Dumper:
    $VAR1 = \{ '001' => { 'Name' => 'AAA' }, '002' => { 'Name' => 'BBB' } };
    My question now is, how I can correctly assign the keys '001', '002', to the correct values 'AAA', 'BBB', respectively?
      See my above answer about .xls vs .xlsx files. What are you asking now is a simple hash operation: you must assign to each key the inner contained value:

      # quotes needed even with fat comma or +you get 1 instead of 001 perl -MData::Dumper -e "%hash=('001'=>{Name=>'AAA'}); foreach $k (keys + %hash){$hash{$k}=$hash{$k}{Name}} print Dumper \%hash" $VAR1 = { '001' => 'AAA' };

      L*

      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      Hi, at a complete guess I would try changing your call to use the 'Name' column as the key of the hash. Untested; see what you get:

      $excel_table->parse_table("Sheet1", "Name") ;
      ... or, if that doesn't work, you can manipulate the hash you already have:
      my $hashref = $excel_table->get_table("ID"); my %excel_hash = %{ $hashref }; my %new_hash = map { $_ => $excel_hash{ $_ }->{'Name'} } keys %excel_h +ash;
      (map loops throuugh the keys of the hash, assigning each key to $_. In the block we dig down and get the value we want and match it to the key in the new hash we create).

      Hope this helps!


      The way forward always starts with a minimal test.
        Hi lnickt, thanks much!... but i apologize if i still not able to get it resolved... been frying my brain on the correct syntax to extract the hash value but im having issue using the correct variable in terms of scalar, etc.
        foreach my $keys1 (keys %{$excel_hash}) { print "$keys1\n"; foreach my $keys2 (keys %{%$excel_hash{$keys1}}) { print "$keys2\n"; my $value = %{%$excel_hash{$keys1}{$keys2}}; } }
        From the above code, everything works without the line where i assign the value to $value. Output below:
        $VAR1 = \{ '002' => { 'Name' => 'BBB' }, '001' => { 'Name' => 'AAA' } }; 002 Name 001 Name
        With that $value line, i have syntax error on that line. Please help...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1199333]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2017-11-25 06:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:













    Results (355 votes). Check out past polls.

    Notices?