Perl Hash of Hashes

From GM-RKB
Jump to navigation Jump to search

A Perl Hash of Hashes is a hash of hashes data structure and also a perl hash data structure.



References

2011

  • (GM-RKB, 2011-08-30) ⇒ Gabor Melli. (2011). “Perl Hash of Hashes - Examples". GM-RKB
Object Creation
  • Manual
   my %HoH = (
     flintstones ⇒ {
        lead      ⇒ "fred",
        pal       ⇒ "barney",
     },
     jetsons ⇒ {
        lead      ⇒ "george",
        wife      ⇒ "jane",
        "his boy" ⇒ "elroy", # key quotes needed
     },
     simpsons ⇒ {
        lead      ⇒ "homer",
        wife      ⇒ "marge",
        kid       ⇒ "bart",
     },
   )
  • Via references
   $requiredPatches_href->{ $patch }->{ os }    = $os;
   $requiredPatches_href->{ $patch }->{ arch }  = $arch;
   $requiredPatches_href->{ $patch }->{ info }  = $info;
  • From file
   # flintstones: lead=fred pal=barney wife=wilma pet=dino
   while (<> ) {
      next unless s/^(.*?):\s*'' ;
      $who = $1;
      for $field (split ) {
         ($key, $value) = split /=/, $field;
         $HoH{$who}{$key} = $value;
      }
   }
From file (with more temporary variables)
   while (<> ) {
      next unless s/^(.*?):\s*'' ;
      $who = $1;
      $rec = {};
      $HoH{$who} = $rec;
      for $field (split ) {
         ($key, $value) = split /=/, $field;
         $rec->{$key} = $value;
      }
   }
Calling a function that returns a key,value
  • for the inner hash
   for $group ("simpsons", "jetsons", "flintstones" ) {
      $HoH{$group} = { get_family($group) };
   }
  • using temporary variables
   for $group ("simpsons", "jetsons", "flintstones" ) {
      %members = get_family($group);
      $HoH{$group} = { %members };
   }
Calling a function that returns the outer hash
  • including references to the created inner hashes
   sub hash_families {
      my @ret;
      foreach $group (@_ ) {
         push @ret, $group, { get_family($group) };
      }
      @ret;
   }
   %HoH = hash_families( "simpsons", "jetsons", "flintstones" );
  • append new members to an existing family
   %new_folks = (
      wife ⇒ "wilma",
      pet  ⇒ "dino";
   )

   for $what (keys %new_folks) {
      $HoH{flintstones}{$what} = $new_folks{$what};
   }
Access and printing of a hash of hashes ==
  • one element
   print $HoH{flintstones}{wife} ;

   # another element
   $HoH{jetsons}{'his boy'} =~ s/(\w)/\u$1/;

   # print the whole thing
   foreach $family (keys %HoH ) {
      print "$family: ";
      foreach $role (keys %{ $HoH{$family} } ) {
         print "$role=$HoH{$family}{$role} ";
      }
      print "\n";
   }

   # print the whole thing
   foreach $family (keys %HoH ) {
      print "$family: ";
      foreach $role (keys %{ $HoH{$family} } ) {
         $ref =  \%{$HoH{$family}} ;
         print "$role=$ref->{$role} ";
      }
      print "\n";
   }

   # print the whole thing, using temporaries
   while (($family,$roles) = each %HoH ) {
      print "$family: ";
      while (($role,$person) = each %$roles ) {  # using each precludes sorting
         print "$role=$person ";
      }
      print "\n";
   }

   # print the whole thing somewhat sorted
   foreach $family (sort keys %HoH ) {
      print "$family: ";
      foreach $role (sort keys %{ $HoH{$family} } ) {
         print "$role=$HoH{$family}{$role} ";
      }
      print "\n";
   }

   # print the whole thing sorted by number of members
   foreach $family (sort { keys %{$HoH{$a}} <=> keys %{$HoH{$b}} } keys %HoH ) {
      print "$family: ";
      foreach $role (sort keys %{ $HoH{$family} } ) {
         print "$role=$HoH{$family}{$role} ";
      }
      print "\n";
   }

Establish a sort order (rank) for each role
   $i = 0;
   for (qw(lead wife son daughter pal pet) ) { $rank{$_} = ++$i }

   # now print the whole thing sorted by number of members
   foreach $family (sort { keys %{$HoH{$a}} <=> keys %{$HoH{$b}} } keys %HoH ) {
      print "$family: ";

      # and print these according to rank order
      foreach $role (sort { $rank{$a} <=> $rank{$b} } keys %{ $HoH{$family} } ) {
         print "$role=$HoH{$family}{$role} ";
      }
      print "\n";
   }
   IV CLASS="sect2" >