ankit.tayal560 has asked for the wisdom of the Perl Monks concerning the following question:

Student name IQ sports Music Academic Azhar x x x x Jason x x Gurmeet x x x x Vishal x x x

Stated above is my excel file. I need to convert this into a hash table and later access the contents-> student's name and his quality for which 'x' is marked. any suggestions how should I do it? I am using Spreadsheet::ParseExcel module to read through excel files.

  • Comment on How to convert Excel contents into a hash table and further access the contents of that hash table ?
  • Download Code

Replies are listed 'Best First'.
Re: How to convert Excel contents into a hash table and further access the contents of that hash table ?
by Happy-the-monk (Canon) on Oct 05, 2016 at 06:07 UTC

    I need to convert this into a hash table and later access the contents-> student's name and his quality for which 'x' is marked. any suggestions how should I do it?

    The Synopsis of https://metacpan.org/pod/Spreadsheet::ParseExcel gives you an example.

    How did that not help?

    Or is it that Perl hashes are new to you? Read up on perlintro and expecially in the part about hashes in Variable types.

    Once the first and second part are understood, you will find a third layer or dimension is needed. You have a multitude of ways to do that. I'd suggest you use a hash of hashes for that to start with.

    Cheers, Sören

    Créateur des bugs mobiles - let loose once, run everywhere.
    (hooked on the Perl Programming language)

Re: How to convert Excel contents into a hash table and further access the contents of that hash table ?
by Laurent_R (Canon) on Oct 05, 2016 at 06:27 UTC
    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.
      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,

        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

      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!

Re: How to convert Excel contents into a hash table and further access the contents of that hash table ?
by shadowsong (Pilgrim) on Oct 05, 2016 at 15:08 UTC

    Hi ankit.tayal560,

    You need to break up your get_cell subroutine calls to avoid processing blank cells i.e. change every instance of:

    $key_var = $worksheet->get_cell($x,$y)->value

    to something like

    $cell = $worksheet->get_cell($x,$y); next unless $cell; $key_var = $cell->value;

    Additionally, I would change for my $row(1..$worksheet->row_range) to for my $row(1..($worksheet->row_range)[1]) - the row_range subroutine returns a list, not a scalar

    I hope that helps, good luck!
    Shadowsong

      thanks for the guidance. it did work! thanks again