Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: How to remove duplicate key/value pairs in hash of array

by dws (Chancellor)
on Feb 20, 2001 at 23:20 UTC ( #59740=note: print w/ replies, xml ) Need Help??


in reply to How to remove duplicate key/value pairs in hash of array

You can eliminate duplicate values by inverting the hash (turning key/value pairs into value/key pairs) and then re-inverting.

%hash = (a => 1, b => 1, c => 2); %hash = reverse %hash; # key/value -> value/key %hash = reverse %hash; foreach $key ( keys %hash ) { print $key, " => ", $hash{$key}, "\n"; }


Comment on Re: How to remove duplicate key/value pairs in hash of array
Download Code
Replies are listed 'Best First'.
Re^2: How to remove duplicate key/value pairs in hash of array
by bdalzell (Sexton) on Feb 04, 2009 at 15:18 UTC
    this works to remove duplicate values but if the keys are numeric it removes the lower numbered key value pairs. how would you remove the higher key numbered key value pairs retaining the key/value pair with the lowest value key?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://59740]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (12)
As of 2015-07-29 20:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (269 votes), past polls