Feed on

trying to push values onto my hash of arrays (push($hash{$key},value);), i kept receiving the following error:

Type of arg 1 to push must be array

turns out i needed to explicitly cast my hash value as an array, using @{}:


Bookmark and Share

if that was helpful ...

check out the other tips and tricks i've compiled on these pages. you might learn something else interesting!

3 Responses to “explicitly cast variable as an array in perl”

  1. on 31 Jul 2008 at 4:58 pm Bill

    Thanks for this. I got hung up with a similar problem.
    Mine was a hash of structs of which each included an array.

    Saved me from beating my head in :)

  2. on 01 Jul 2009 at 12:10 am Toxoplasma

    Isn’t working for me D:
    My code:
    push( @{$markov{$fullwords}}, $words[$c + $order]);

    print “$fullwords :: ” . $words[$c + $order] . ” :: ” . $markov{$fullwords}[@markov{$fullwords} - 1] . “\n”;

    $fullwords and $words[$c + $order] print fine… Exactly what they should be. They just aren’t getting pushed onto the matrix, it seems…

  3. on 14 Jul 2014 at 7:59 am Avidan Efody

    Works well

Did I get this wrong? Let me know!

Trackback URI | Comments RSS