Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1035335]
help
Chatterbox?
[Lady_Aleena]: Would someone please derail my train of thought?
[Discipulus]: marto as Marcus Manlius Capitolinus
[Discipulus]: use Dynamite; Lady_Aleena
[Lady_Aleena]: That's the problem. I have 29 scripts, 3 modules, and gads of data I am thinking about deleting out of anger over an API change that made all of those things worthless.

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2017-05-25 07:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?