in reply to How to convert Excel contents into a hash table and further access the contents of that hash table ?

In which part of this are you experiencing difficulties? Please show your code attempts, so that we can help you without spoon-feeding you with a complete solution for a probably homework.
  • Comment on Re: How to convert Excel contents into a hash table and further access the contents of that hash table ?

Replies are listed 'Best First'.
Re^2: How to convert Excel contents into a hash table and further access the contents of that hash table ?
by ankit.tayal560 (Beadle) on Oct 05, 2016 at 07:03 UTC
    use strict; use Spreadsheet::ParseExcel; use Data::Dumper; use warnings; my $parser=Spreadsheet::ParseExcel->new(); my $workbook=$parser->parse('C:\Perl\perl_tests\Sample.xls'); if(!defined $workbook) { die $parser->error(),".\n"; } my $worksheet=$workbook->worksheet('Sheet1'); my %student_data; for my $row(1..$worksheet->row_range) { my $super_key=$worksheet->get_cell($row,0)->value; for my $col(0..4) { my $key=$worksheet->get_cell(0,$col)->value; my $value=$worksheet->get_cell($row,$col)->value; ##here I am getting a problem## $student_data{$super_key}->{$key}=$value; } } foreach my $student_name(sort keys %student_data) { foreach my $attribute (keys%{$student_data{$student_name}}) { if($student_data{$student_name}{$attribute} eq "x") { print("$student_name-->$attribute\n"); } } print("\n"); }

    it shows an error "can't call method 'value' on an undefined value. I guess it is because in some cells 'x' is not written and hence that cell is empty so it is showing me this error. I tried it when all the cells contain 'x' then it is working. What should I do in this case?

      Hello ankit.tayal560,

      Are you sure the error is coming from the code shown? When I run your code, accessing an Excel file created according to the table in the OP, it works without errors:

      17:20 >perl 1704_SoPW.pl Azhar-->Academic Azhar-->IQ Azhar-->Music Azhar-->sports Gurmeet-->Academic Gurmeet-->IQ Gurmeet-->Music Gurmeet-->sports Jason-->IQ Jason-->Music Vishal-->Academic Vishal-->IQ Vishal-->sports 17:21 >

      It may help if you detail your platform, Perl version, and Spreadsheet::ParseExcel version. Here are mine:

      • Windows 8.1, 64-bit
      • Strawberry Perl 5.22.1
      • Spreadsheet::ParseExcel 0.65

      Also, please copy and paste the exact error message you receive (within <code> ... </code> tags).

      Cheers,

      Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

        Yes error is coming when I run the above code, plateform which I am using is : (1) Windows 7 (2) Active state perl 5.8.9 build 825 (3) Spreadsheet::ParseExcel 0.65

        exact error message is : Can't call method "value" on an undefined value at C:\perl\perl_tests\ +excel_parser.pl line 22
      for my $row (1 .. $worksheet->row_range) { my $super_key = $worksheet->get_cell ($row, 0)->value; for my $col (0 .. 4) { my $key = $worksheet->get_cell (0, $col)->value; my $value = $worksheet->get_cell ($row, $col)->value; # here I am getting a problem # $student_data{$super_key}->{$key} = $value; } }

      I am pretty sure the error if from either of the two lines above that. get_cell (...) will return undef for undefined cells, for which you cannot use the value method.


      Enjoy, Have FUN! H.Merijn
Re^2: How to convert Excel contents into a hash table and further access the contents of that hash table ?
by ankit.tayal560 (Beadle) on Oct 05, 2016 at 08:28 UTC

    Hi Laurent_R could you please look at other comments posted by me and tell me a suitable solution for the above problem. I've enlisted the code which I've tried as well. thanks!

        Hi Marto, Yes I've read that and understand the importance of posting the trial code. I just was not sure about correctness of the code which I wrote so did not post it. but yes I do understand I should've posted it in the first place. now please if can u help me on this I would be very glad. Thanks in advance