Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Another "Can't use string as an ARRAY ref"

by bradcathey (Prior)
on Dec 05, 2016 at 13:46 UTC ( [id://1177209]=perlquestion: print w/replies, xml ) Need Help??

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


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


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++; } } } }


[ { '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' } ];
"The important work of moving the world forward does not wait to be done by perfect men." George Eliot

Replies are listed 'Best First'.
Re: Another "Can't use string as an ARRAY ref"
by hippo (Bishop) on Dec 05, 2016 at 14:07 UTC

    Your useful print statement shows you that $plants->[$i]{'common'} has value "State Street Miyabe Maple". What then do you intend this expression from the following line to do?


    That's clearly the equivalent of:

    "State Street Miyabe Maple"->[$jctr]{'sizes'}

    which is nonsense. You cannot dereference a scalar string like that. Does that make it clearer to you?

      Brilliant. I'm a visual learner and this was very helpful. Now to figure out a solution. More to come...

      "The important work of moving the world forward does not wait to be done by perfect men." George Eliot

        You might want to consider a simpler more SQL solution

        #!perl use strict; use DBI; use Data::Dumper; my $dbh = create_db(); my $sql = 'SELECT P.common_name,S.plant_size,S.unit,G.abbr FROM plants as P LEFT JOIN sizes as S ON S.plantid = LEFT JOIN growers as G ON S.plantid = G.plantid AND S.plant_size = G.plant_size'; my $sth = $dbh->prepare($sql); $sth->execute(); my %hash=(); while (my @f = $sth->fetchrow_array){ $hash{$f[0]}{$f[1].$f[2]}{$f[3]} = 1 } print Dumper(\%hash);
Re: Another "Can't use string as an ARRAY ref"
by haukex (Archbishop) on Dec 05, 2016 at 14:02 UTC

    Hi bradcathey,

    One of the first things you do in your loop is assign $plants->[$i]{'common'} =  $plants->[$i]{'common_name'};, but then later on you attempt to use $plants->[$i]{'common'}, which now holds a string, as an array ref when you write $plants->[$i]{'common'}->[$jctr]{'sizes'} = ..., that's where the error is coming from. Are you perhaps using the hash key common twice by accident?

    Hope this helps,
    -- Hauke D

Re: Another "Can't use string as an ARRAY ref"
by choroba (Cardinal) on Dec 05, 2016 at 14:06 UTC
    OK, so you first assign a name, so probably a string to $plants->[$i]{common} :
    $plants->[$i]{'common'} = $plants->[$i]{'common_name'};

    And few lines later, you try to store something in a hash ref:

    $plants->[$i]{'common'}->[$jctr]{'sizes'} = $sizes->[$j]{'plantsiz +e'}.$sizes->[$j]{'unit'}; # ~~~~~~~~~~~~~~~~~~~~~~~

    The underlined thing is a string (comment mine). You can't use a string as a hash ref under strict.

    BTW, is $jctr ever different to $j (and $kctr to $k )? If not, you can improve readability by dropping two variables.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1177209]
Approved by haukex
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2024-05-25 08:59 GMT
Find Nodes?
    Voting Booth?

    No recent polls found