Iterate through Hash of Hashes
Here's how you would iterate through multidimensional hashes in Perl:
Create a hash first:
#!/usr/bin/perl
use warnings;
use strict;
my %HoH = (
flintstones => {
husband => "fred",
wife => "wilma",
pal => "barney",
},
jetsons => {
husband => "george",
wife => "jane",
"his boy" => "elroy", # Key quotes needed.
},
simpsons => {
husband => "homer",
wife => "marge",
kid => "bart",
},
);
Then iterate through it:
foreach my $family (keys %HoH) {
while (my ($key, $value) = each %{ $HoH{$family} } ) {
print "$key = $value \n";
}
}
Other operations
Insert another set of values:
$HoH{ mash } = {
captain => "pierce",
major => "burns",
corporal => "radar",
};
Access particular value:
print $HoH{flintstones}{wife}; # will output "wilma"
Set value of a particular key:
$HoH{$flintstones}->{wife} = "jane"; #give Fred a new wife
while (my ($key, $value) = each %{ $hash{$family} } ) {
uses $hash, where it should be using $HoH. $hash doesn't seem to be defined or set elsewhere.