Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Find all keys from a value in a hash

by haukex (Bishop)
on Nov 09, 2021 at 21:16 UTC ( #11138651=note: print w/replies, xml ) Need Help??


in reply to Find all keys from a value in a hash

There's More Than One Way To Do It: If you need to do a lot of lookups, then grep will quickly become inefficient, and instead it will likely be worth it to build a reverse lookup table (assuming it fits comfortably into memory). With the following, you can simply write $colors{green}, $colors{red}, etc.

use warnings; use strict; my %fruit = ( apple => ['red','green'], kiwi => 'green', banana => 'yellow', ); my %colors; for my $f (keys %fruit) { for my $c ( ref $fruit{$f} ? @{ $fruit{$f} } : $fruit{$f} ) { push @{ $colors{$c} }, $f; } } use Data::Dump; dd \%colors;

By the way, always Use strict and warnings!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2022-01-22 17:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (63 votes). Check out past polls.

    Notices?