Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

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?

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2015-11-28 03:38 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (737 votes), past polls