Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl "Not an ARRAY reference" error

Tags:

hash

perl

I'll be glad if someone can enlighten me as to my mistake:

my %mymap; 
@mymap{"balloon"} = {1,2,3};

print $mymap{"balloon"}[0] . "\n";
like image 613
eve Avatar asked Dec 06 '22 19:12

eve


1 Answers

$mymap{'balloon'} is a hash not an array. The expression {1,2,3} creates a hash:

 {
   '1' => 2,
   '3' => undef
 }

You assigned it to a slice of %mymap corresponding to the list of keys: ('balloon'). Since the key list was 1 item and the value list was one item, you did the same thing as

$mymap{'balloon'} = { 1 => 2, 3 => undef };

If you had used strict and warnings it would have clued you in to your error. I got:

Scalar value @mymap{"balloon"} better written as $mymap{"balloon"} at - line 3. Odd number of elements in anonymous hash at - line 3.

like image 58
Axeman Avatar answered Jan 04 '23 16:01

Axeman