Solved

# Hash of Hashes

Posted on 2007-07-19
364 Views
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 => {
pal       => "barney",

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

},
jetsons     => {
wife      => "jane",
"his boy" => "elroy",

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

},
simpsons    => {
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
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 "} ";
}else{
print "\$role=\$HoH{\$family}{\$role} ";
}

}
print "}\n";
}
0

Author Comment

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

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

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:

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";
}

0

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

## Featured Post

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 (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…
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…