Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Replace a hash key if it matches a regex

by Happy-the-monk (Monsignor)
on Sep 13, 2013 at 08:48 UTC ( #1053868=note: print w/ replies, xml ) Need Help??


in reply to Replace a hash key if it matches a regex

Say the hash key is BLAH01-A05AFGH. I want the hash key to be BLAH01-A

perl -e'use Data::Dumper; %hash=("BLAH01-A05AFGH0"=>17); for(keys %hash) { if (m/(BLAH\d{2}-\w)/) { $hash{$1} = delete $hash{$_} } }; print Dumper \%hash'

$VAR1 = { 'BLAH01-A' => 17 };

NB there's no check if the new key already exists... it will be overwritten if it does.

Cheers, Sören

Créateur des bugs mobiles - let loose once, run everywhere.
(hooked on the Perl Programming language)


Comment on Re: Replace a hash key if it matches a regex
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2014-08-31 08:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (294 votes), past polls