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

UPDATE: Worked it out! Typically, as soon as I pressed "submit"!!!!

The issue was that I was doing = instead of ,:

push @{$invoice_items->{$tmp}} = {

should have been:

push @{$invoice_items->{$tmp}}, {

Ok,I must be getting worn out and missing something stupid here! I have the following code, which takes some values out of a DB, and if it finds 2 entries with the same ID, it puts them into an arrayref (so I can loop them later)

while (my $invoice = $sth->fetchrow_hashref) { my $tmp = $invoice->{amazon_order_id}; $tmp =~ s/ \(\d+\)//; # get rid of (0) (1) etc at the end of th +e id, as we dont need it here print qq|GOT: $invoice->{amazon_order_id} ($tmp) \n|; if ($invoices->{$tmp}) { print STDERR "TEST: " . ref($invoice_items->{$tmp}) . "\n"; push @{$invoice_items->{$tmp}} = { name => $invoice->{product_name}, qty => $invoice->{quantity_shipped}, amount => $invoice->{item_price}, total => $invoice->{item_price} * $invoice->{quantity_s +hipped}, }; print STDERR "added.. \n"; } else { $invoices->{$tmp} = $invoice; push @{$invoice_items->{$tmp}}, { name => $invoice->{product_name}, qty => $invoice->{quantity_shipped}, amount => $invoice->{item_price}, total => $invoice->{item_price} * $invoice->{quanti +ty_shipped}, }; print Dumper($invoice_items); } }


For some reason, it just doesn't like it though!

When I run it, I get this (in the part where its trying to `push` a value into the arrayref, not create it);

TEST: ARRAY Not an ARRAY reference at make.cgi line 34.


As you can see, the line:

print STDERR "TEST: " . ref($invoice_items->{$tmp}) . "\n";

... is giving back ARRAY, so why on earth won't it let me do this:

push @{$invoice_items->{$tmp}} = { ... vals }


ARGH!

Replies are listed 'Best First'.
Re: Not an ARRAY reference, even though it is?
by neilwatson (Priest) on Aug 25, 2015 at 16:09 UTC
      Thanks - will try and remember that one :)
Re: Not an ARRAY reference, even though it is?
by 1nickt (Canon) on Aug 25, 2015 at 16:11 UTC

    Do you have use warnings; at the top of your program? It appears not, as this code:

    [nick:~/monks]$ perl -Mstrict -Mwarnings -e" my @foo; push @foo = {bar=>'baz'}; "
    produces this output for me:
    Useless use of push with no values at -e line 2. push on reference is experimental at -e line 2. Not an ARRAY reference at -e line 2.
    As you can see the first line of the output is more descriptive about the error, which often helps you find it more quickly. So always use warnings;.

    As an aside: You don't show what you do with your array, but I am wondering if it might not be more useful for you to have your invoices stored in a hash so you can retrieve them by index:

    #! perl use strict; use warnings; my $cast = list_characters()->{'cast'}; print "The Human is $cast->{'Human'}->{'fname'} $cast->{'Human'}->{'ln +ame'}."; print " He is always displaying $cast->{'Human'}->{'characteristic'}.\ +n"; sub list_characters { my $Guide; $Guide->{'cast'}->{'Human'} = { fname => 'Arthur', lname => 'Dent', characteristic => 'bewilderment', }; $Guide->{'cast'}->{'Betelgeusian'} = { fname => 'Zaphod', lname => 'Beeblebrox', characteristic => 'narcissism', }; $Guide->{'cast'}->{'Robot'} = { fname => 'Marvin', lname => 'the Paranoid Android', characteristic => 'depression', }; return $Guide; } __END__

    Output:

    [09:07][nick:~/monks]$ perl 1139851.pl The Human is Arthur Dent. He is always displaying bewilderment.

    The way forward always starts with a minimal test.
      Thanks - yeah I have strict and warnings enabled :)

      For the code - thats how I ended up doing it. I only put it into a new var after having the issue with the "not an ARRAY" issue. After I got that figured out, I moved it all back into the main hash (along with all the order details)

      Thanks!
Re: Not an ARRAY reference, even though it is?
by stevieb (Canon) on Aug 25, 2015 at 15:32 UTC