Hash of Hashes

Posted on 2007-07-19
Last Modified: 2010-03-05
This is an example from perldoc .I also modify the example cretaed new hashes name Mount1,2,3 .
Need to output current Hash

%HoH = (
        flintstones => {
                lead      => "fred",
            pal       => "barney",

                Mount1 => {
                             'A'  => '1',

        jetsons     => {
            lead      => "george",
            wife      => "jane",
            "his boy" => "elroy",

                Mount2  => {
                             'B'  => '2',

        simpsons    => {
            lead      => "homer",
            wife      => "marge",
            kid       => "bart",

                 Mount3  => {
                             'C'  => '3',


 print Dumper \%HoH;

 print "\n *********************************  \n";

 # print the whole thing
 foreach $family ( keys %HoH ) {
     print "$family: { ";
     for $role ( keys %{ $HoH{$family} } ) {
         print "$role=$HoH{$family}{$role} ";
         print "$family1: { ";
     for $role1 ( keys %{ $HoH{$family}{$family1} } ) {
         print "$role1=$HoH{$family}{$family1}->{$role1} ";

     print "}\n";

# I am using below method to display hash and key getting trouble with display
# Need to fix below Foreach loop to get output
## Output I am getting :: How to get correct output

simpsons: { kid=bart : { lead=homer : { wife=marge : { Mount3=HASH(0x18abdc8) : { }
jetsons: { his boy=elroy : { lead=george : { wife=jane : { Mount2=HASH(0x18abe04) : { }
flintstones: { lead=fred : { Mount1=HASH(0x225238) : { pal=barney : { }

Question by:gauravflame
    LVL 84

    Accepted Solution

    foreach $family ( keys %HoH ) {
         print "$family: { ";
         for $role ( keys %{ $HoH{$family} } ) {
             if( ref $HoH{$family}{$role} eq 'HASH' ){
             print "$role: { ";
               for $role1 ( keys %{ $HoH{$family}{$role} } ) {
                 print "$role1=$HoH{$family}{$role}{$role1} ";
             print "} ";
               print "$role=$HoH{$family}{$role} ";

         print "}\n";

    Author Comment

    if( ref $HoH{$family}{$role} eq 'HASH' ){

    Here 'HASH is a key word
    What 'HASH'  represent here ?
    LVL 84

    Expert Comment

    perldoc -f ref
           ref EXPR
           ref     Returns a non-empty string if EXPR is a reference, the empty
                   string otherwise. If EXPR is not specified, $_ will be used.
                   The value returned depends on the type of thing the reference
                   is a reference to.  Builtin types include:


                   If the referenced object has been blessed into a package, then
                   that package name is returned instead.  You can think of "ref"
                   as a "typeof" operator.

                       if (ref($r) eq "HASH") {
                           print "r is a reference to a hash.\n";
                       unless (ref($r)) {
                           print "r is not a reference at all.\n";
                       if (UNIVERSAL::isa($r, "HASH")) {  # for subclassing
                           print "r is a reference to something that isa hash.\n";

                   See also perlref.
    LVL 39

    Expert Comment

    Because you have 2 types of values for role, you need to check what type it is and handle them different.
    The type in {flintstones}->{lead} is a scalar (plain value), while the type in {flintstones}->{Mount1} is a reference to anohter hash.  That is why ozo is using the ref.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
    A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (,  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now