Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Find common values in a hash and print the respective keys

by johngg (Abbot)
on May 26, 2013 at 19:12 UTC ( #1035335=note: print w/ replies, xml ) Need Help??


in reply to Find common values in a hash and print the respective keys

Reverse the hash into a HoA and select those with multiple values in the array.

$ perl -MData::Dumper -Mstrict -Mwarnings -E ' my %h = ( aa => 1, bb => 2, cc => 3, dd => 4, ee => 2, ff => 5, gg => 3, hh => 2, ); my %r; push @{ $r{ $h{ $_ } } }, $_ for keys %h; print Data::Dumper->Dumpxs( [ \ %r ], [ qw{ *r } ] ); say qq{Value $_ present in keys @{ [ join q{, }, @{ $r{ $_ } } ] } } f +or grep { @{ $r{ $_ } } > 1 } keys %r;' %r = ( '1' => [ 'aa' ], '4' => [ 'dd' ], '3' => [ 'gg', 'cc' ], '2' => [ 'bb', 'ee', 'hh' ], '5' => [ 'ff' ] ); Value 3 present in keys gg, cc Value 2 present in keys bb, ee, hh $

I hope this is helpful.

Cheers,

JohnGG


Comment on Re: Find common values in a hash and print the respective keys
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2015-07-31 06:22 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 (274 votes), past polls