Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

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)

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 meditating upon the Monastery: (8)
As of 2016-10-29 00:45 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (387 votes). Check out past polls.