[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Hash of Hashes

Posted on 2007-07-19
4
Medium Priority
?
370 Views
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 : { }



0
Comment
Question by:gauravflame
  • 2
4 Comments
 
LVL 85

Accepted Solution

by:
ozo earned 2000 total points
ID: 19528686
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 "} ";
        }else{
           print "$role=$HoH{$family}{$role} ";
        }

     }
     print "}\n";
 }
0
 

Author Comment

by:gauravflame
ID: 19528732
if( ref $HoH{$family}{$role} eq 'HASH' ){

Here 'HASH is a key word
or
What 'HASH'  represent here ?
0
 
LVL 85

Expert Comment

by:ozo
ID: 19529863
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:

                   SCALAR
                   ARRAY
                   HASH
                   CODE
                   REF
                   GLOB
                   LVALUE

               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.
0
 
LVL 39

Expert Comment

by:Adam314
ID: 19531453
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.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  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…
Six Sigma Control Plans
Suggested Courses

873 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