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


In reply to Re: How to grep for values & see the keys? by kcott
in thread How to grep for values & see the keys? by misterperl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.