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)
|
|---|