Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: do something if hash keys match regex

by shmem (Chancellor)
on Jul 20, 2017 at 08:44 UTC ( [id://1195571]=note: print w/replies, xml ) Need Help??


in reply to do something if hash keys match regex

There is no way to select hash keys with a regular expression other than iterating over the keys, be that done with a for/foreach loop, map, or a statement modifier. So, no, you can't do that without using foreach and braces.

But you could stuff your regular expression components into another hash, to avoid repeating yourself inside the foreach loop

my %re = ( boom => [ qw( abc ghi) ], doom => [ qw( mmm nnn) ], zapp => [ qw( xxx yyy) ], ); foreach my $key (keys %hash) { $key =~ /$re{$_}->[0]_(.*)_$re{$_}->[1]/ and $hash{$key} = $_.$1 for keys %re; }

which arguably isn't as readable as your current solution.

perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1195571]
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: (5)
As of 2024-04-23 21:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found