A Hash of Arrays (1) is a Hash, where the keys are strings and the values references to anonymous arrays. You can declare a Hash Of Arrays as %HoA.
Recall to construct an anonymous array reference with square brackets: []
$anon_array_ref = ['scalar_1', 'scalar_2', 'scalar_3'];
You can extend the existing hashes by new key/value pairs:%HoA = ( # hash of arrays; notice the open and close parenthesis: it's a real hash!<
'Sebastian' => ['red', 'blue'],
'Daniel' => ['brown', 'green'],
'Florence'=> ['blue', 'black', 'orange'],
);
$HoA{'Kirsten'} = ['green','yellow'];
%HoA_copy = %HoA;
%HoA_combi = (%HoA1, %HoA2);
%HoA_copy = ();
@color = @{$HoA{'Kirsten'}};
print("@color\n");# output: green yellow
print("$color[1]\n");# output: yellow
Printing values:%HoA_copy = %HoA;
foreach (sort(keys(%HoA_copy))) {
print "$_ ";# output: Daniel Florence Kirsten Sebastian
}
Printing keys and values:%HoA_copy = %HoA;
foreach (sort(values(%HoA_copy))) {
print "@$_ ";
}
The output is:%HoA_copy = %HoA;
foreach $name (sort(keys(%HoA_copy))) {
foreach $color (sort(@{$HoA_copy{$name}})) {
print "$name $color\n";
}
}
Daniel brown
Daniel green
Florence black
Florence blue
Florence orange
Kirsten green
Kirsten yellow
Sebastian blue
Sebastian red
Alternatively, you could do the following:$HoA{'Bibian'} = ['red', 'green','blue'];
To extend an existing array, you could use the push command.@mc_array = qw(green blue black);
$HoA{'Bibian'} = \@mc_array;
push(@{$HoA{'Sebastian'}},'black');
delete($HoA{'Sebastian'}) if (exists $HoA{'Sebastian'});
$HoA{'Sebastian'} = ['pink', 'yellow', 'purple'];