A Hash of Hashes (1) is a Hash, where the keys are strings and the values references to anonymous hashes. You can declare a Hash Of Arrays as %HoH.
Recall that members of %name_hash are accessed by $name_hash{$key}
Recall how to construct an anonymous hash reference with curly braces {}:
$anon_hash_ref = {'key1' => 'value1', 'key2' => 'value2'};
An easier alternative:%HoH = ( # hash of hashes; notice the open and close parenthesis: it's a real hash!<
'hash1' => {
'key1' => '1-111',
'key2' => '1-222',
},
'hash2' => {
'key1' => '2-111',
'key2' => '2-222',
},
);
You can extend the existing hashes by new key/value pairs:%hash1 = ();
$hash1{'key1'} = '1-111';
$hash1{'key2'} = '1-222';
%hash2 = ();
$hash2{'key1'} = '2-111';
$hash2{'key2'} = '2-222';
%HoH = ( 'hash1' => \%hash1, 'hash2' => \%hash2 );
$hash1{'key3'} = '1-333';
$hash2{'key3'} = '2-333';
%HoH_copy = %HoH;
%HoH_combi = (%HoH1, %HoH2);
%HoH_copy = ();
%HoH_copy = %HoH;
print("{'hash1'}{'key2'}");# output: 1-222
Printing keys and values:%HoH_copy = %HoH;
foreach (sort(keys(%HoH_copy))) {
print("$_ ");# output: hash1 hash2
}
The output is:%HoA_copy = %HoA;
foreach $hash (sort(keys(%HoH_copy))) {
foreach $key (sort(keys %{$HoH_copy{$hash}})) {
print("hash - $key: $HoH_copy{$hash}{$key}\n");
}
}
hash1 - key1: 1-111
hash1 - key2: 1-222
hash2 - key1: 2-111
hash2 - key2: 2-222
$HoH{'hash1'}{'key3'} = '1-333';
%HoH_copy = %HoH;
delete($HoH_copy{'hash2'}{'key2'}) if (exists $HoH_copy{'hash2'}{'key2'});
%HoH_copy = %HoH;
$HoH_copy{'hash1'}{'key1'} = '4-444'