Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
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 cooling their heels in the Monastery: (9)
As of 2014-09-22 20:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (200 votes), past polls