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

Find all the keys associated with the same value in a hash

Status
Not open for further replies.

LenLindquist

IS-IT--Management
Oct 17, 2002
31
US
Hello,

I'm looking for an elegant solution to finding all of the keys in a hash that have the same value. I have a rather large hash of approximately 700,000 keys with values that may or may not unique to the key.

In other words, it is possible in my hash that the same value may be assigned to more than one key.

The end result I would like to have is a report showing all of the keys that share the same value. For this, I would like to sort the report by value and list all of the keys for each value.

Presently, the solution I have is to intermediately make a file of all of the matched values and then search through the entire hash for each of them to find the matching keys. Obviously this is time consuming and not the least bit "elegant!" I suspect my intermediate step is not even necessary.

Can anyone assist?

Thanks in advance!
Len Lindquist
 
One way to do this is to invert the hash and use the values as keys in a new hash that each point to an array of all the keys that contain that value. I don't know how efficient this is. If i come up with something better, i'll post back.
Code:
# Create a hash of arrays: 
# values of original hash are keys of new hash 
# with arrays as values.
# push keys from original hash with corresponding value onto this array
while ( my ($key, $value) = each %hash ) {

    push @{$invhash{$value}}, $key;
}
# print out in a suitable format
foreach (keys %invhash) {

    print "$_:\n";
    print "\t$_\n" for @{$invhash{$_}};
}
jaa
 
Thank you for the help.

Using your answer as a guide, I've now got a solution that works pretty well.

I did invert the hash as you say, but instead of making a hash of arrays, I simplified a little bit and just separated my values with ":" and used join/split.

To save a little memory I first deleted all the hash keys that had only one value (no dupes). This seemed to make the computer much happier...

That's what I was looking for! Thank you!!!

Len
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top