explicitly cast variable as an array in perl
January 8th, 2007 by Lawrence David
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 @{}:
push(@{$hash{$key}},value);
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
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…
Works well