5

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";
2
  • 4
    You really need to read some documentation on Perl and Perl data structures: read perldoc perlintro, perldoc perldata and probably perldoc perlreftut and perldoc perldsc as well. Commented Aug 17, 2011 at 14:40
  • 3
    Further there are books available like Modern Perl available. Please learn the language basics and come back to us when you have bugs and we will be glad to help! Commented Aug 17, 2011 at 14:42

4 Answers 4

6

$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.

Sign up to request clarification or add additional context in comments.

Comments

5

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";

Comments

3

Well, first off, always use strict; use warnings;. If you had, it might have told you about what is wrong here.

Here's what you do in your program:

my %mymap;  # declare hash %mymap
@mymap{"balloon"} = {1,2,3};  # attempt to use a hash key on an undeclared 
                              # array slice and assign an anonymous hash to it

print $mymap{"balloon"}[0] . "\n";  # print the first element of a scalar hash value

For it to do what you expect, do:

my %mymap = ( 'balloon' => [ 1,2,3 ] );
print $mymap{'balloon'}[0];

Comments

1

Okay, a few things...

%mymap is a hash. $mymap{"balloon"} is a scalar--namely, the value of the hash %mymap corresponding to the key "balloon". @mymap{"balloon"} is an attempt at what's called a hash slice--basically, you can use these to assign a bunch of values to a bunch of keys at once: @hash{@keys}=@values.

So, if you want to assign an array reference to $mymap{"balloon"}, you'd need something like:

$mymap{"balloon"}=[1,2,3].

To access the elements, you can use -> like so:

$mymap{"balloon"}->[0] #equals 1
$mymap{"balloon"}->[1] #equals 2
$mymap{"balloon"}->[2] #equals 3

Or, you can omit the arrows: $mymap{"balloon"}[0], etc.

3 Comments

N.B. Between indexes you may omit the arrow operator -> as per the "Arrow Rule" in perldoc perlreftut to say $mymap{ballon}[0]
Yeah, I personally prefer using the arrows, since (IMO) they allow an easier visual demarcation of the layers in the data. Since TIMTOWTDI, however, I've made an edit.
I understand and even support that point, however once you have several indexes lined up most readers should see that it is a nested data structure and be ok with it. BTW I do usually still use an arrow when the data contained is a coderef that I am invoking.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.