Solved

# Hash

Posted on 2007-07-26
Medium Priority
250 Views
%HoH = (

main => "school",

flintstones => {
pal       => "barney",

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

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

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

},
simpsons    => {
wife      => "marge",
kid       => "bart",

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

},
);

#########Part A start ###################
foreach \$family ( keys %HoH ) {
print "\$family: { ";
if( ref \$HoH{\$family} eq "HASH" ){
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} ";
}

} else{
print "\$family = \$HoH{\$family}";
}
print "}\n";
}
#################Part A ends ###############3
=========================================
Recursive function::How I can write recursice funciton for part B from Part A?
----Start B Here ---------------
repeathashfunction(%HoH)
foreach \$family ( keys %HoH ) {
if (ref \$family eq 'HASH')
{
repeathashFunction(%HoH);
}
elsif (ref \$family  eq 'Scalar')
{
print " \$key = \$value ";
}
else
{
print "\$family = \$HoH{\$family}";
}

Question ::

0
Question by:gauravflame

LVL 17

Assisted Solution

mjcoyne earned 600 total points
ID: 19581436
I'm not sure what you're asking.  What do you intend repeathashfunction(%HoH) to do?  It looks like you want to write a sub function, and be able to call it repeatedly?  Something like:

#!/usr/bin/perl -w
use strict;

my %HoH = (
main => "school",
flintstones => {
pal => "barney",
Mount1 => {
'A' => '1',
},
},
jetsons => {
wife => "jane",
"his boy" => "elroy",
Mount2  => {
'B' => '2',
},
},
simpsons => {
wife => "marge",
kid => "bart",
Mount3 => {
'C' => '3',
},
},
);

repeathashfunction(\%HoH);  # first call

print "\n************** First call done **************\n\n";

repeathashfunction(\%HoH);  # re-call the same function

sub repeathashfunction {
my \$hash_ref = shift;
foreach my \$family (keys %{\$hash_ref}) {
print "\$family: { ";
if (ref \$hash_ref->{\$family} eq "HASH" ){
for my \$role (keys %{\$hash_ref->{\$family}}) {
if (ref \$hash_ref->{\$family}{\$role} eq 'HASH' ){
print "\$role: { ";
for my \$role1 (keys %{\$hash_ref->{\$family}{\$role}} ) {
print "\$role1=\$hash_ref->{\$family}{\$role}{\$role1} ";
}
print "} ";
} else {
print "\$role=\$hash_ref->{\$family}{\$role} ";
}
}
} else {
print "\$family = \$hash_ref->{\$family}";
}
print "}\n";
}
};

I assume you're learning Perl -- I recognize the 'flinstones' hash.  That's great!  Here are some important tips -- please get used to properly indenting your code structures:  it makes unbalanced brackets and such quite easy to spot.

Also, you *must* force yourself to start every script with 'use strict' and 'use warnings' -- this will save you tons of time in the long run, by spotting errors for you.

A good IDE is also very helpful, because it will do indenting for you, and has syntax highlighting, tips on function use, etc.  I use a purchased copy of Komodo, but there's a free version available (see http://www.activestate.com/Products/komodo_edit/).

Hope this helps...
0

LVL 39

Accepted Solution

ID: 19582409
The "use strict" and "use warnings" from mjcoyne are vey good - they will help in the long run.

As for the function, are you looking for a recursive function to display a hash-of-hash?  If so, here's one:

repeathashfunction(\%HoH);

sub repeathashfunction {
my \$refh=shift;
my \$ind=(shift or '');

return unless ref(\$refh) eq 'HASH';
print "{\n";
\$ind .= "  ";
foreach my \$key (keys %{\$refh}) {
print "\$ind\$key => ";
if(ref(\$refh->{\$key}) eq "HASH") {
repeathashfunction(\$refh->{\$key}, \$ind);
}
else {
print "\$refh->{\$key},\n";
}
}
\$ind = substr(\$ind, 0, length(\$ind)-2);
print "\$ind}\n";
}
0

## Featured Post

Question has a verified solution.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
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
Course of the Month13 days, 16 hours left to enroll