eve eve - 5 days ago 5
Perl Question

perl: error not an ARRAY reference

I'm pretty new with Perl and I'm trying to do this very very simple thing and I'll be glad if someone can enlighten me with my mistake:

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


print $mymap{"balloon"}[0] . "\n";


What is it??

Thanks!

Answer

If you had used 'use strict; use warnings;' on the top of your code you probably have had better error messages.

What you're doing is creating a hash called mymap. A hash stores data as key => value pairs. You're then assigning an array reference to the key balloon. Your small code snipped had two issues: 1. you did not addressed the mymap hash, 2. if you want to pass a list, you should use square brackets:

my %mymap;
$mymap{"balloon"} = [1,2,3];
print $mymap{"balloon"}[0] . "\n";

this prints '1'.

You can also just use an array:

my @balloon = (1,2,3);
print $balloon[0] . "\n";