http://qs321.pair.com?node_id=1177209

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

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