Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

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

by tybalt89 (Monsignor)
on May 13, 2022 at 17:18 UTC ( [id://11143885]=note: print w/replies, xml ) Need Help??


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

Using 'map' as a 'grep' :)

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11143876 use warnings; my %hash; @hash{1..26} = 'a' .. 'z'; my %want = map { $hash{$_} =~ /c|x/ ? ($_, $hash{$_}) : () } keys %has +h; use Data::Dump 'dd'; dd \%want;

Outputs:

{ 3 => "c", 24 => "x" }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (1)
As of 2024-04-24 23:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found