Array in value of hash perl
Yes it is. Create a reference to the array by using backslash:
$hash{key} = \@array;
Note that this will link to the actual array, so if you perform a change such as:
$array[0] = "foo";
That will also mean that $hash{key}[0]
is set to "foo"
.
If that is not what you want, you may copy the values by using an anonymous array reference [ ... ]
:
$hash{key} = [ @array ];
Moreover, you don't have to go through the array in order to do this. You can simply assign directly:
$hash{key} = [ qw(foo bar baz) ];
Read more about making references in perldoc perlref
Yes. See http://perlmonks.org/?node=References+quick+reference for some basic rules for accessing such data structures, but to create it, just do one of these:
%hash = ( 'somekey' => \@arrayvalue );
$hash{'somekey'} = \@arrayvalue;
%hash = ( 'somekey' => [ ... ] );