http://qs1969.pair.com?node_id=1057442

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

I have been stuck on this problem for weeks now. I am reading a XLSX workbook and need to read F28 to F32 through K28 to K32 into variables, and I have not been able to figure out how to write a routine to do this.

I have:

$book = ReadData ($inbook); my @x = qw(F G H I J K); for my $sheet_index (1 .. $sheet_count) { my $sheet = $book->[$sheet_index] or next; foreach (@x) { $name = $sheet{"$_28"}; $strain = $sheet{"$_29"}; $initdensity = $sheet{"$_30"}; $finaldensity = $sheet{"$_31"}; $avedensity = $sheet{"$_32"}; print "Found sheet with label: $sheet{label}\n"; print "COL=$_ $name $strain $initdensity $finaldensity $avedens +ity\n"; } }

If hard code the reference as $sheet{"F28"} for instance, it works. Iterating through row numbers, however, does not.

Can anyone provide guidance on how?

Thanks,

Hammer.

Replies are listed 'Best First'.
Re: Iterating Through Cell Blocks with Spreadsheet::Read
by toolic (Bishop) on Oct 08, 2013 at 17:30 UTC

    Tip #1 from the Basic debugging checklist: use strict and warnings. You get plenty of helpful information when you do. Perl thinks you have a variable named $_28. However, you really want to concatenate the string in $_ with the string 28. Here is one way to fix your problem:

    $name = $sheet{"${_}28"};
      Thanks you for your reply, toolic. I still get the same error "Use of uninitialized value in concatenation (.) or string at..." on the print statement after replacing lines like "$name = $sheet{"$_28"}" with "$name = $sheet{"${_}28"}". Any other thoughts? Regards, Hammer.
        I hate to post this because it sounds condescending -- I don't mean it that way, honest!

        You know you have to do this with all the lines where you have such a hash key (e.g. the ones with "_29", "_30", etc), right? Not just the _28 one.

        I've been trying to get toolic's suggestion to not work, here, and I haven't succeeded.

Re: Iterating Through Cell Blocks with Spreadsheet::Read
by kcott (Archbishop) on Oct 09, 2013 at 08:13 UTC

    G'day Hammer2001

    I thought this looked familiar.

    "I have been stuck on this problem for weeks now."

    When you asked about this some weeks ago, in Spreadsheet::XLSX Cell Access, you were shown different ways to fix code like "$_NN". You acknowledged the information and said thankyou; however, you appear to have ignored the advice.

    Perhaps revisiting that thread might be a good start. :-)

    -- Ken

      Anomolous, thank you for the guidance. Referencing the hash using statements like '$name = $sheet->{"${_}28"}' did the trick! Gratefully, Hammer.