Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-25 14:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found