Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Replace a hash key if it matches a regex

by Happy-the-monk (Abbot)
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 about the Monastery: (7)
As of 2015-07-05 05:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (60 votes), past polls