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

Exploring Hash of Hash

Status
Not open for further replies.

facemann

Programmer
May 19, 2005
9
US
I am curious.
I have created a Hash of Hash as follows:
$HoH{$last_name}{$first_name}{$birth_day}{$phone_num} = "$id_num";
I often know the values of ($last_name, $first_name) and would like to find the
$birth_day value for this individual.
Is there an elegant way to do this?

Naturally:
print "$HoH{$last_name}{$first_name}"; #only prints a memory address.

My solution was to use keys:
for my $birthday(keys %{$HoH{$last_name}{$first_name} } ) {
my $key_id = $HoH{$last_name}{$first_name};
print "Birthday = $birthday\n";
}


Is there a simpler soulution? Thanks for any suggestions as I spent considerable time fighting this! :)
 
Whenever you find yourself treating keys as values, realize that it's a BadIdea(tm).

For this case, I would do:
Code:
my %foo = (
    $id => {
        last  => 'harrison',
        first => 'andy',
        phone => 'myphone',
        dob   => 'bday'
    }
);

Then you can do things like:
Code:
print $foo{$id}{last};

You can also easily iterate all your id's and grab individual values or whatever you like.

--
Andy
"Historically speaking, the presence of wheels in Unix has never precluded their reinvention."
Larry Wall
 
Thanks for the reply naChoZ.
Your %foo hash is a valid solution. However, I needed the multilevel hash because I was checking each person for duplicates. I am parsing a file for a database.
But, you have hit the nail on the head with your comment "treating keys as values". That's exactly what I was doing and why I was so frustrated. Ah, why does the learning curve have to be UPHILL? ;-)
Thanks again.
 
Simple enough. Just search the monastary for hash duplicates or something like that and you'll see plenty of responses.

--
Andy
"Historically speaking, the presence of wheels in Unix has never precluded their reinvention." -- Larry Wall
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top