1

I am a newbie in Perl and trying out few stuffs.One thing I noticed is if one wants to pass an array as a value of a hashmap key one way of doing it is to pass it by reference. As follows

$hash_map{key} = \@arr

Is there any way to pass the array by value directly? Thanks

2 Answers 2

5

A hash maps strings (keys) to scalar values. A value in a hash can only ever be a scalar.

An array is not (by definition!) a scalar. The best way to get a scalar value which represents that array is to take the reference to that array. And that's what your code does. This is how you should do it.

There are other ways to create a scalar value to represent an array. You could, for example, use join() to create a string from the elements of the array. But that would be very fragile as you would need to find a separator character that doesn't appear in any of the elements.

Far better to just take a reference as you are already doing.

Update: To clarify, there are three way to do this with references.

  • $hash{key} = \@array - takes a reference to the array and stores the reference in $hash{key}.
  • $hash{key} = [ @array ] - unrolls the array into a list, creates a new array using that list as the elements and returns a reference to this new array (effectively copying the array).
  • @{$hash{key}} = @array - this also takes a copy of the array and stores a reference to the copy.
Sign up to request clarification or add additional context in comments.

Comments

1

The following works for me just fine under perl 5.10 and 5.14:

use strict;
use Data::Dumper;

my @array= qw(foo 42 bar);
my %hash;
@{ $hash{key} } = @array;
$hash{key} = [ @array ]; #same as above line
print Dumper(\%hash,$hash{key}[1]);

outputs (your order may vary):

$VAR1 = {
          'key' => [
                     'foo',
                     '42',
                     'bar'
                   ]
        };
$VAR2 = '42';

I prefer the @{ $hash{key} } syntax because you can push/pop with it, i.e.

push @{ $hash{key} }, "value";

which is surprisingly handy (for me anyway)
also, with this syntax you are copying the array and not just putting a reference to it, meaning you can change the original array without impacting the hash

19 Comments

That syntax works. And, as you can see, it does exactly the same thing as $hash{key} = \@list. But which option is easier to understand? @{ $hash{key} } looks pretty noisy to me :-)
@DaveCross I don't think it is quite the same as it's copying the array
@DaveCross thanks for clearing it up. i edited the answer to reflect why i prefer my syntax
@ChrisTurner: Yes. you're right. @{$hash{key}} = @array is the same as $hash{key} = [ @array ], but (slightly) different to $hash{key} = \@array.
@Nullman: @list is a terrible name for that variable. It's an array, not a list :-)
|

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.