I'm trying to create a hash in Perl, whose values are arrays. Something like:
my @array = split(/;/, '1;2');
my $hash = {'a' => @array};
Surprisingly, this reports (using Data::Dumper):
$VAR1 = {
'a' => '1',
'2' => undef
};
This page gives an example of storing an array in a hash by defining the array use square brackets, like:
my $hash = {'a' => ['1', '2']};
That works, but I'm getting my array from a call to the split
method. What's magic about square brackets versus parentheses for defining an array, and how can I turn a "parentheses-array" into a 'square-brackets' array?
The values of hash (and array) elements are scalars, so you can't store an array into a hash.
The following are all equivalent:
A common solution is to store a reference to the array.
[ LIST ]
similarly create an array, assignsLIST
to it, then returns a reference to the array.