Fellow Monasterians,

I'm building a complicated nested loop, or an AOH of AOH of AOH for an Excel::Template file but getting the proverbial:

Can't use string ("State Street Miyabe Maple ") as an ARRAY ref while "strict refs in use  line 74"
when building my third loop. Strange because I'm done this many times before, but this one must be different.

I have a list of plants from one DB table, that I use to get a list of sizes from another table, that I use to get a list of growers that stock those plants for even another table (I did not build the DB). So, I want to end up with...

Roses-> 2" -> MA CD TH 4" -> CD TH Violets-> 3" -> MA TH etc.

I've read several nodes on the topic here at the Monastery and elsewhere, but the light hasn't gone on. I know it has something to do with a scalar vs. an array ref, but I'm not seeing it here. Can someone help me get my head around the concept? Thanks!

...query statements built, etc., and get plants AOH... print "plants: ".Dumper($plants); for my $i (0..$#$plants) { $plants->[$i]{'common'} = $plants->[$i]{'common_name'}; #query the database for available sizes of the plants $sizes = $self->dbh->selectall_arrayref($s_stmt, {Slice => {}},($p +lants->[$i]{'id'}, 1)); print "sizes: ".Dumper($sizes); if (@{$sizes}) { $jctr = 0; for my $j (0..$#$sizes) { #j-loop print "j-loop: ". $plants->[$i]{'common'}."\n"; line 74: $plants->[$i]{'common'}->[$jctr]{'sizes'} = $sizes->[$j]{' +plantsize'}.$sizes->[$j]{'unit'}; #query the database for growers with those plants $growers = $self->dbh->selectall_arrayref($g_stmt, {Slice +=> {}}, ($sizes->[$j]{'plantid'}, $sizes->[$j]{'plantsize'}, 1)); print "growers: ".Dumper($growers); #haven't gotten this far yet if (@{$growers}) { $kctr = 0; for my $k (0..$#$growers) { $plants->[$i]{'common'}->[$jctr]{'sizes'}->[$kctr] +{'growers'} = $growers->[$k]{'two_letter_abbrev'}; $kctr++; #increase counter } #for growers } #if growers $jctr++; #increase counter } #for sizes } #if sizes } #for plants loop

Dumper

plants: $VAR1 = [ { 'genus' => 'Acer miyabei \'Morton\'', 'common_name' => 'State Street Miyabe Maple', 'type' => '1', 'chicagoland_grows' => '0', 'id' => '1063' } ]; sizes: $VAR1 = [ { 'show_plant' => '1', 'plantsize' => '1.5-2', 'plantid' => '1063', 'unit' => '"', 'userid' => '4' } ]; j-loop: State Street Miyabe Maple

UPDATE:

Thanks for helping me get the concept.

for my $i (0..$#$plants) { $sizes = $self->dbh->selectall_arrayref($s_stmt, {Slice => {}},($p +lants->[$i]{'id'}, 1)); if (@{$sizes}) { $jctr = 0; for my $j (0..$#$sizes) { $plants->[$i]{'sizes'}->[$jctr]{'size'} = $sizes->[$j]{'pl +antsize'}.$sizes->[$j]{'unit'}; $jctr++; #query just the growers with those plants, by size + $growers = $self->dbh->selectall_arrayref($g_stmt, {Slice +=> {}}, ($sizes->[$j]{'plantid'}, $sizes->[$j]{'plantsize'}, $sizes-> +[$j]{'unitid'},1)); if (@{$growers}) { $kctr = 0; for my $k (0..$#$growers) { $plants->[$i]{'sizes'}->[$j]{'growers'}->[$kctr]{' +abbrev'} .= $growers->[$k]{'two_letter_abbrev'}." "; + } $kctr++; } } } }

Output.

[ { 'sizes' => [ # $i - loop { 'size' => '5-6"', 'growers' => [ # $j - loop { 'abbrev' => 'KT ' # $k - loop } ] } ], 'chicagoland_grows' => '0', 'common_name' => 'Northern Pin Oak ', 'id' => '7108', 'genus' => '', 'type' => '1' }, { 'sizes' => [ { 'growers' => [ { 'abbrev' => 'KN ' } ], 'size' => '1.5-4.5"' }, { 'growers' => [ { 'abbrev' => 'HI ' } ], 'size' => '2-4"' } ], 'genus' => 'Acer nigrum \'Green Column\'', 'chicagoland_grows' => '1', 'common_name' => 'Green Column Black Maple', 'id' => '6', 'type' => '1' } ];
—Brad
"The important work of moving the world forward does not wait to be done by perfect men." George Eliot

In reply to Another "Can't use string as an ARRAY ref" by bradcathey

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.