I have following hash of hash :
%HoH = (
flintstones => {
husband => "fred",
pal => "barney",
},
jetsons => {
husband => "george",
wife => "jane",
his boy => "elroy",
},
simpsons => {
husband => "homer",
wife => "marge",
kid => "bart",
},
);
How to iterate over each inner hash (say flintstones) and also extract the key names (husband, pal) and corresponding vales for each such iteration?
another way would be using
each
Because each key in the outer level points to a hash in the inner level we can use all the normal hash functions on that entry.
While it is possible to write this in a more succinct way without the double loop I prefer the nested loops for two reasons.
It is more obvious when someone else has to work on the code (including you in six months as someone else).
It makes it easier to track things such as which outer key leads to this point if needed (as shown in my output).
Just loop over the hash (by
keys
orvalues
oreach
, depending on whether you need the keys and on taste) and then loop over the inner hashes (likewise).So, to get all of the people described by this hash:
Or to build a table of all the husbands, all the wives, etc.:
Or to re-key the table off the husbands: