Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

2 hashes 2 one hash 1

Status
Not open for further replies.

nfaber

Technical User
Oct 22, 2001
446
US
Forgive the newbie question,but I am stumped!

How can I take two hashes, combine them to make a hash of hash, and then loop through the hash of hash one slice at a time and print out the variables.

Thanks
 
Let me get this straight... You want to
1. combine two hashes into one
2. loop through hash one and print the keys and values
Am I right?

//Daniel
 
Thanks for the reply.

Yes, I want to combine two separate hashes into one hash of hash, and then loop through the hash of hash and print out each slice using a write statement.
 
Something like this....?

==================================================
#!/usr/bin/perl

%hash1=(
a => 1,
b => 2
);


%hash2=(
c => 3,
d => 4
);

%hashhash= (
rh1 => \%hash1,
rh2 => \%hash2
);


while(($key,$val) = each (%hashhash) )
{

print "hash $key\n";

while(($key1 => $val1) = each (%$val) )
{
print "$key1 => $val1\n";
}

print "\n\n";
}

===============================================


regards
C "Brahmaiva satyam"
-Adi Shankara (788-820 AD)
 
Latch,

Thanks for the reply! Can you humor a newbie here and explain a few things. If my hashes are already populated, why am I assigning them to what looks like different values with:

%hash1=(
a => 1,
b => 2
);


%hash2=(
c => 3,
d => 4
);

If they are already populated in my script can I skip the above?

Also for this part:

%hashhash= (
rh1 => \%hash1,
rh2 => \%hash2
);
why do I need to backslash the hash variable name?

Also, I am not looking to print out %hashhash but input each slice to a write statement. Is that as simple as replacing the prints with a write? Also, in my format for the write statement, what variables should I use?

Also finally, is there a tutorial somewhere that explains all this? I have three perl books I am using to learn and none seem to explain it so I can understand it easily.

Sorry for all the extra questions, but I really want to learn thuis stuff!

Thanks again.
 
>>If they are already populated in my script can I skip the above?

Yes .


>>why do I need to backslash the hash variable name?


Thats the way to reference a hash.
References are Perl's equivalent for pointers in C.

For a start on References , try 'perldoc perlreftut' & 'perldoc perlref' on your prompt.



>>I am not looking to print out %hashhash but input each >>slice to a write statement. Is that as simple as >>replacing the prints with a write? Also, in my format for >>the write statement, what variables should I use

Im not sure what needs to be accomplished here ...maybe some examples might help.

>>Also finally, is there a tutorial somewhere that explains >>all this?

Yes ....Sriram Srinivasan's Advanced Perl Programming is really good and sufficient for References


Also 'perldoc perllol', 'perldoc perldsc' 'perldoc perlfaq4' on your prompt might help.



regards
C

"Brahmaiva satyam"
-Adi Shankara (788-820 AD)
 
Thanks again for the reply latch. I will check out those docs (nice weekend reading). As far as and example of what I am trying to do:

here is the code I have so far:

foreach (@reseast) {
($ipeast,$stateast) = /(\d+\.\d+\.\d+\.\d+).*?\s+(\w+)\Z/;
$reseast = inet_aton("$ipeast");
$hosteast = gethostbyaddr($reseast, AF_INET);
$anseast=$hosteast || $ipeast;
$haeast{$anseast} = $stateast;
}


foreach (@reswest) {
($ipwest,$statwest) = /(\d+\.\d+\.\d+\.\d+).*?\s+(\w+)\Z/;
$reswest = inet_aton("$ipwest");
$hostwest = gethostbyaddr($reswest, AF_INET);
$answest=$hostwest || $ipwest;
$hawest{$answest} = $statwest;
}


Above is how I am populating my hashes

Below is the format I am looking for for the output:

format STDOUT_TOP =
------------ -------------
.

format STDOUT =
@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<
$anseast $stateast $answest $statwest
.


I want to print out for each slice:

$haeast{$anseast}, $haeast{$stateast}, $hawest{answest}, $hawest{$statwest}

I am almost there, just can't get it to work!
 
Follow-up to your code
Im just mentioning the syntax here


==============================================

# Adding Entries for Hash

$netmap{'East'}=\%haeast;
$netmap{'West'}=\%hawest;





# Parsing Each entry in new Hash

while(($direction,$dir_ref) = each (%netmap) )
{

print &quot;$direction\n&quot;;




# $dir_ref is a reference to a hash ....need to find keys & # values


# To access the Hash referenced by $dir_ref , use %$dir_ref

while(($host_or_ip,$stat) = each (%$dir_ref) )
{
print &quot;$host_or_ip => $stat\n&quot;;
}

print &quot;\n\n&quot;;
}

==============================================





HTH
C


&quot;Brahmaiva satyam&quot;
-Adi Shankara (788-820 AD)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top