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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hash of Hashes

Status
Not open for further replies.

perln00b

Programmer
Oct 4, 2005
21
US
I am new in Perl script. I get a real headache to understand
"hash of hashes". Could someone please explain to me? Here is some example codes:
"...
if($tempids{'IP'}{ $IP { $hostname } }{ $resourceID } !=1 )
{
$add_detail{'IP'}{$resourceID}{$IP{$hostname}} = 1;
}
...
"

Thanks,
Lucas
 
Code:
my %Flintstones = (
   Fred => { kids => 'Pebbles',
             wife => 'Wilma',
             pet => 'Dino',
           },
   Barney => { kids => 'Bam-Bam',
               wife => 'Betty',
               pet => 'None',
              },
);

print "Fred's wife is $Flintstones{Fred}{wife}.
Barney's wife is $Flintstones{Barney}{wife}.
Fred has a pet dinosuar named $Flintstones{Fred}{pet}.
Their childrens names are $Flintstones{Fred}{kids} and $Flintstones{Barney}{kids}.";

prints:

Fred's wife is Wilma.
Barney's wife is Betty.
Fred has a pet dinosuar named Dino.
Their childrens names are Pebbles and Bam-Bam.
 
Just like you enter a tall building on the bottom floor, (the building is the top level thinking in hash terms). You find the floor you need (the second level in hash terms), and finally you find the office you want (the bottom level of the hash). You can do that without even thinking about it!

Clinic -> 2nd Floor -> Office 2B = Dr. Smith

The hash is really just the same:

Flintstones -> Fred -> Wife = Wilma
 
My representation is a tree, which name is the name of the hash and each branch is a key of that hash. If the branch has it's own branches, they also have names and that are the keys of their parent branch.
Try using Data::Dumper or the Perl debuger. to see that visually.

Corwin
 
The thing which may be confusing in your case is that your first key is also hash value.
So $tempids is the tree, $IP { $hostname } is just the name of one of it's branches, and $resourceID is a little branch inheritor of the branch with name $IP { $hostname }. Then we chek if we have a bud on the last branch or something else, which is the value of the hash.

Corwin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top