Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Hash search and returning by value

by Anonymous Monk
on Dec 09, 2014 at 15:43 UTC ( [id://1109761]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!
How can I search on the hash value and also print the value I found if it matches. My test code is giving me the key.
Should I also be using "defined" or "exists" in case nothing is found?
use strict; use warnings; my $zip = '01234'; my %zips = ( "City One" => "04321", "City Two" => "01234", ); my ($zip_match) = grep { $zips{$_} eq $zip } keys %zips; print "\n $zip_match\n"; # 01234

Thanks!

Replies are listed 'Best First'.
Re: Hash search and returning by value
by Eily (Monsignor) on Dec 09, 2014 at 15:46 UTC

    If you're going to do that often, and you can't have two cities with the same zip code, you can reverse the hash like this: my %reversed = reverse %zips; and then try exists $reversed{$zip}. Or you can grep on the values instead of the keys: grep { $_ eq $zip } values %zips;

      Yes, it worked:
      use strict; use warnings; my $zip = '01234'; my %zips = ( "City One" => "04321", "City Two" => "01234", ); my ($zip_match) = grep { $_ eq $zip } values %zips; print "\n $zip_match\n"; # 01234

      Can "grep" take a condition, like only assign to "$zip_match" if it is true?
        Without parens on the LHS you'll call grep in scalar context and get the number of matches returned.

        Only 0 matches is false.

        But you should really take reverse into consideration to create an inverted hash.

        Cheers Rolf

        (addicted to the Perl Programming Language and ☆☆☆☆ :)

        Can "grep" take a condition, like only assign to "$zip_match" if it is true?

        In the statement
            my ($zip_match) = grep { $_ eq $zip } values %zips;
        the condition grep takes is  $_ eq $zip and it matches in list context, so it returns nothing (the empty list) if there is no match and assigns nothing to  $zip_match which retains the uninitialized value (i.e., undef) with which it was created.

Re: Hash search and returning by value
by AnomalousMonk (Archbishop) on Dec 09, 2014 at 16:01 UTC

    In the OPed code, if  $zip is not any value of the hash,  $zip_match will not be defined:

    c:\@Work\Perl\monks>perl -wMstrict -le "my $zip = '99999'; ;; my %zips = ('City One' => '04321', 'City Two' => '01234'); ;; my ($zip_match) = grep { $zips{$_} eq $zip } keys %zips; print qq{'$zip' not found} if not defined $zip_match; " '99999' not found
    (exists will not be appropriate in this case.)

    Update: The result of the grep operation (one or more matches vs. none) can also be tested directly:

    c:\@Work\Perl\monks>perl -wMstrict -le "my $zip = '01234'; ;; my %zips = ('City One' => '04321', 'City Two' => '01234'); ;; if (my ($zip_match) = grep { $zips{$_} eq $zip } keys %zips) { print qq{'$zip' located in '$zip_match'}; } else { print qq{'$zip' not found}; } " '01234' located in 'City Two'

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (9)
As of 2024-03-28 12:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found