Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!
I am trying to read a .XLS file to extract the data from its column and later insert them into a database, but the code is giving me a lot of uninitialized value messages. I just cant see where in the code it could be happening and why, here is the code, unless there is a better way of doing this!
#!/usr/bin/perl -w use strict; use warnings; use Spreadsheet::ParseExcel; my $file = "../my_file.xls"; use vars qw($user_name $user_number $identification); my $workbook = Spreadsheet::ParseExcel::Workbook->Parse($file)or die " +Unable to open $file\n"; foreach my $page (@{$workbook->{Worksheet}}) { print "Page $page\n"; foreach my $col ($page->{MinCol} .. $page->{MaxCol}) { if ($page->{Cells}[0][$col]->{Val} eq "User Name") { $user_name = $col; print "$user_name\n"; } if ($page->{Cells}[0][$col]->{Val} eq "User Number") { $user_number = $col; print "$user_number\n"; } if ($page->{Cells}[0][$col]->{Val} eq "Identification") { $identification = $col; print "$identification\n"; } } foreach my $row ($page->{MinRow}+1 .. $page->{MaxRow}) { my $got_user_name = $page->{Cells}[$row][$user_name]->{Val}; my $got_user_number = $page->{Cells}[$row][$user_number]->{Val}; my $got_identification = $page->{Cells}[$row][$identification]->{V +al}; print "\n$got_user_name\n$got_user_number\n$got_identification\n"; } } exit;


Thanks for looking!

Replies are listed 'Best First'.
Re: Parsing XLS file Help!
by jmcnamara (Monsignor) on Oct 27, 2010 at 16:57 UTC
    You are getting "a lot of uninitialized value messages" because you are accessing a lot of data without checking if it exists or not.

    Also, you are using the, by now, very old interface of Spreadsheet::ParseExcel. Try rewrite your example based on the example at the start of the current docs and employ the same type of error checking. That should ensure that you don't access uninitialised data.

    If you still have a problem after that post your example again. As it is at the moment there are just too many issues to even start fixing the code.

    --
    John.

Re: Parsing XLS file Help!
by larryl (Monk) on Oct 27, 2010 at 16:16 UTC

    You say that the code is giving you a lot of uninitialized value messages - what lines numbers are reported in the messages? That will tell you where you need to be looking...

Re: Parsing XLS file Help!
by Tux (Canon) on Oct 27, 2010 at 18:46 UTC

    Spreadsheet::Read is a wrapper around a variety of Spreadsheet parsing modules, amongst which is Spreadsheet::ParseExcel. Its default behaviour is to clip sheets to not show trailing empty columns and rows. Values like maxrow and maxcol will be adjusted accordingly.

    If you are using perl-5.10 or up, the defined-or operator // might also come in very very handy.


    Enjoy, Have FUN! H.Merijn
Re: Parsing XLS file Help!
by Jim (Curate) on Oct 27, 2010 at 16:25 UTC

    What database software? Microsoft Office Access?

    For what it's worth, I'm solving a problem at work today by using Microsoft Office Excel to store data where it can easily be edited in a controlled way — in a protected worksheet with both locked and unlocked cells — by colleagues who are masterful with Excel, but frightened of Access. I've linked the Excel worksheet to an Access database where my Perl script can much more easily query the data using SQL and the Perl DBI. So I'm leveraging Excel for its ease of user input and maintenance and Access for its ease of data access.

    I've only ever read good things about Spreadsheet::ParseExcel, but I've never had the time to sit down, pore over its fine documentation and study its many terrific examples.

    I'm sorry I didn't answer your question, but hopefully I've suggested something that might be helpful to you now or in the future.

Re: Parsing XLS file Help!
by umasuresh (Hermit) on Oct 27, 2010 at 15:00 UTC
Re: Parsing XLS file Help!
by cjb (Friar) on Oct 27, 2010 at 16:50 UTC
    Have you, by any chance got empty Worksheets in the Workbook?
      If so, the following should work.
      #!/usr/bin/perl -w use strict; use warnings; use Spreadsheet::ParseExcel; my $file = "test.xls"; use vars qw($user_name $user_number $identification); my $workbook = Spreadsheet::ParseExcel::Workbook->Parse($file)or die " +Unable to open $file\n"; foreach my $page (@{$workbook->{Worksheet}}) { print "Page $page\n"; if ((defined $page->{MinCol}) && (defined $page->{MaxCol})) { foreach my $col ($page->{MinCol} .. $page->{MaxCol}) { if ($page->{Cells}[0][$col]->{Val} eq "User Name") { $user_name = $col; print "$user_name\n"; } if ($page->{Cells}[0][$col]->{Val} eq "User Number") { $user_number = $col; print "$user_number\n"; } if ($page->{Cells}[0][$col]->{Val} eq "Identification") { $identification = $col; print "$identification\n"; } } } if ((defined $page->{MinRow}) && (defined $page->{MaxRow})) { foreach my $row ($page->{MinRow}+1 .. $page->{MaxRow}) { my $got_user_name = $page->{Cells}[$row][$user_name]->{Val +}; my $got_user_number = $page->{Cells}[$row][$user_number]-> +{Val}; my $got_identification = $page->{Cells}[$row][$identificat +ion]->{Val}; print "\n$got_user_name\n$got_user_number\n$got_identifica +tion\n"; } } } exit;
        I like that , this will do it! Thanks!