Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: How to grep for values & see the keys?

by kcott (Archbishop)
on May 14, 2022 at 01:31 UTC ( [id://11143889]=note: print w/replies, xml ) Need Help??


in reply to How to grep for values & see the keys?

G'day misterperl,

Hash keys are unique; their values do not have this restriction. Unless you can guarantee unique values, for the entire life of your code, do not use solutions that rely on unique values (e.g. reverse). Consider the different output from these two runs of identical code:

$ perl -e ' use Data::Dump; my %h = qw{cat Tibbles dog Rover kitten Tibbles}; my %r = reverse %h; dd \%h; dd \%r; ' { cat => "Tibbles", dog => "Rover", kitten => "Tibbles" } { Rover => "dog", Tibbles => "cat" } $ perl -e ' use Data::Dump; my %h = qw{cat Tibbles dog Rover kitten Tibbles}; my %r = reverse %h; dd \%h; dd \%r; ' { cat => "Tibbles", dog => "Rover", kitten => "Tibbles" } { Rover => "dog", Tibbles => "kitten" }
"I've never figured out a concise way to do this. ... Obviously it's a simple matter to write a for(){} to do this, but I'm thinking there is some elusive grep that can do it using the hash in list context?"

You may be falling into the trap of thinking shorter code is faster code. In the main, for is faster than grep and map. I've put together a Benchmark example to show this. I haven't used pairmap or pairgrep previously, so a added a pairmap to the benchmark (which turned out to be even slower). Do note that this is just an example; you should modify this to more accurately reflect the code you're working with.

#!/usr/bin/env perl use strict; use warnings; use Benchmark 'cmpthese'; use List::Util 'pairmap'; use Data::Dump; my %h = qw{mammal cat mollusc snail fish cat}; print "*** Check initial data\n"; dd \%h; print "*** Check subs return the same value\n"; print "_for\n"; dd _for(); print "_map\n"; dd _map(); print "_pairmap\n"; dd _pairmap(); print "*** Benchmark\n"; cmpthese 0 => { for => \&_for, map => \&_map, pairmap => \&_pairmap, }; sub _for { my $result = []; for my $key (keys %h) { next unless $h{$key} eq 'cat'; push @$result, $h{$key}, $key; } return $result; } sub _map { my $result = []; @$result = map { $h{$_} eq 'cat' ? ($h{$_}, $_) : () } keys %h; return $result; } sub _pairmap { my $result = []; @$result = pairmap { $b eq 'cat' ? ($b, $a) : () } %h; return $result; }

The output from the initial checks was identical on each run, so I'll just show that once.

*** Check initial data { fish => "cat", mammal => "cat", mollusc => "snail" } *** Check subs return the same value _for ["cat", "fish", "cat", "mammal"] _map ["cat", "fish", "cat", "mammal"] _pairmap ["cat", "fish", "cat", "mammal"]

I discarded the output of the first benchmark run because the initial loading of modules can skew the results. Here's the results of the next three runs:

*** Benchmark Rate pairmap map for pairmap 775527/s -- -33% -42% map 1154992/s 49% -- -14% for 1346695/s 74% 17% -- *** Benchmark Rate pairmap map for pairmap 779518/s -- -34% -43% map 1177598/s 51% -- -13% for 1360223/s 74% 16% -- *** Benchmark Rate pairmap map for pairmap 781507/s -- -34% -43% map 1178381/s 51% -- -14% for 1364313/s 75% 16% --

— Ken

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-03-28 13:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found