25539

How to extract key name from a hash of hash?

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?

Answer1:

for my $k (keys %{ $HoH{flintstones} }) { my $v = $HoH{flintstones}{$k}; print "key is $k; value is $v\n"; }

another way would be using each

while( my($k, $v) = each %{ $HoH{flintstones} }) { ... }

Answer2:

for my $outer_key ( keys %HoH ) { print "Values for inner_hash $outer_key:\n"; for my $inner_key ( keys %{$HoH{$outer_key}} ) { print "\t'$inner_key' points to " . $HoH{$outer_key}{$inner_key} . "\n"; } }

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.

<ol> <li>

It is more obvious when someone else has to work on the code (including you in six months as someone else).

</li> <li>

It makes it easier to track things such as which outer key leads to this point if needed (as shown in my output).

</li> </ol>

Answer3:

Just loop over the hash (by keys or values or each, 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:

for (values %HoH) { for (values $_) { push @people, $_ } }

Or to build a table of all the husbands, all the wives, etc.:

for my $fam (values %HoH) { push @{$relations{$_}}, $fam->{$_} for keys $fam }

Or to re-key the table off the husbands:

for my $fam (keys %HoH) { $patriarchs{$HoH{$fam}{husband}}{family} = $fam; for (keys $HoH{$fam}) { next if $_ eq 'husband'; $patriarchs{$HoH{$fam}{husband}}{$_} = $HoH{$fam}{$_}; } }

Recommend

  • SQL Server 2008 joining
  • How do I properly format a readonly @TextBoxFor Value as currency?
  • LTE signal ASU level always 97
  • How to get nodes with same name and same attribute name to collection?
  • How to check whether C# DateTime is within a range
  • C3.js change label position of Line Chart Y axis to center up of Y axis
  • need help on simple Java pattern coding
  • SQL database problems with addressbook table design
  • Postgresql left outer join on json array
  • Rails: Include all of model A and only a subset of related model B
  • Hibernate discriminator column with table per subclass
  • PostgreSQL: update with left outer self join ignored
  • MySQL Invalid query: Too high level of nesting for select
  • Get Distinct rows from a result of JOIN in SQL Server
  • How do I conditionally select a field from one of two tables?
  • Crafting a LINQ based solution to determine if a set of predicates are satisfied for a pair of colle
  • how to remove a div with same ids but display='block' and display='none' in JAVa
  • Iterate twice through a DataReader
  • Google Places API - Find a company's CID and LRD
  • How do I remove all but some records based on a threshold?
  • Basic many-to-many left join query
  • msbuild create itemgroup from property group
  • Updating both a ConcurrentHashMap and an AtomicInteger safely
  • calculate gradient output for Theta update rule
  • Array with custom indexes in Ionic2
  • Installed module is empty
  • MongoDb aggregation
  • Converting a WriteableBitmap image ToArray in UWP
  • Update CALayer sublayers immediately
  • Check if a string to interpolate provides expected placeholders
  • Javascript + PHP Encryption with pidCrypt
  • align graphs with different xlab
  • Return words with double consecutive letters
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • Recursive/Hierarchical Query Using Postgres
  • Reading document lines to the user (python)
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • Python/Django TangoWithDjango Models and Databases