http://www.perlmonks.org?node_id=1057462


in reply to Re^8: Iterating Through Cell Blocks with Spreadsheet::Read
in thread Iterating Through Cell Blocks with Spreadsheet::Read

Anomolous, Data::Dumper works nicely (when used correctly). It shows the expected values in the format:
'F29' => '1',
though I can make no sense of the order in which it displays contents. I have:
my %sheet;
declaration though it's not initialized. Initialization with:
my %sheet = ();
doe not make a difference in the result.

Thanks,

Hammer.

Replies are listed 'Best First'.
Re^10: Iterating Through Cell Blocks with Spreadsheet::Read
by AnomalousMonk (Archbishop) on Oct 08, 2013 at 22:15 UTC

    Further to Anonymonk's point:

    >perl -wMstrict -le "use Data::Dump; ;; my $sheet = { qw(F29 foo G30 bar) }; dd $sheet; ;; my %sheet; ;; print qq{'$sheet->{F29}'}; print qq{'$sheet{G30}'}; " { F29 => "foo", G30 => "bar" } 'foo' Use of uninitialized value $sheet{"G30"} in concatenation (.) or strin +g at -e line 1. ''

    Do you see how the expressions  $sheet->{F29} and  $sheet{G30} access two completely different structures, one of which is empty?

    ... I can make no sense of the order in which it displays contents.

    A hash has no internal "order" except for the pairing of each unique key with its value. Access to and listing of hash keys, values and key/value pairs is (apparently) random. (Update: Don't be mislead by the fact that the order in the dump displayed above matches the order of initialization of the hash reference example: this is entirely adventitious. The dump of a larger hash structure will show greater disorder — but again, a key always cleaves to its value.)

      Yes, of course. Thanks for the clarification, AnomalousMonk. Now it makes. sense.

      My objective, once again, is to access F29 through F32 to K29 through K32, in a routine. I can't figure how to iterate over rows through a variable. It only works for me when hash key is explicit (i.e. $name = $sheet{"F28"};). How can I use a variable instead of "F28"?

      Thanks,

      Hammer.

        How can I use a variable instead of "F28"?

        This essentially just goes back to toolic's original reply: make sure that  $sheet is a hash reference and that its content is what you expect; access the contents via  -> arrow notation, forming the hash keys as before with something like  $name = $sheet->{"${_}28"}; etc. (A simple concatenation like  $sheet->{$_ . '28'} would work just as well.)

        Here's the problem restated another way. Here's a script to read Excel XLSX cell values using Spreadsheet::Read:
        #!/usr/bin/perl -w use warnings; use strict; use Spreadsheet::Read; my $name = ""; my $strain = ""; my $initdensity = ""; my $finaldensity = ""; my $avedensity = ""; my $book; my $sheet; my %sheet = (); $book = ReadData ($inbook); my @x = qw(F G H I J K); for my $sheet_index (1 .. $sheet_count) { $sheet = $book->[$sheet_index] or next; foreach (@x) { $name = $sheet{'F28'}; $strain = $sheet{'F29'}; $initdensity = $sheet{'F30'}; $finaldensity = $sheet{'F31'}; $avedensity = $sheet{'F32'}; print "Found sheet with label: $sheet{label}\n"; print "COL=$_ $name $strain $initdensity $finaldensity $avedens +ity\n"; } }
        I need to after reading keys F28..F32 read G28..G32 through K28..K32. How can I key it on values from array @x?

        Thanks, Hammer

Re^10: Iterating Through Cell Blocks with Spreadsheet::Read
by Anonymous Monk on Oct 08, 2013 at 21:51 UTC
    And why would an empty has contain something? It wouldn't right, its empty :)