the usual
inconsistent at best
Renaming Hash Keys

Say you have a hash:

my %hash = (foo => 'fooey',
            bar => 'barey',
            baz => 'bazey',
            blech => 'blechey');

and you want to rename some of the hash keys, create another hash that holds the new names of the keys:

## old-name => new-name
my %new = (foo => 'foooo',
           bar => 'baaar');

Then this is a fast way to rename the keys. Measured on 2.4Ghz i5 MacBook Pro.

## 431034.48/s
@hash{values %new} = delete @hash{keys %new};

Here are a few other ways. Copy whole hash:

## 404858.30/s
%hash = map { $new{$_} || $_ => $hash{$_} } keys %hash;

Change only keys found in %new:

## 390625.00/s
$hash{$new{$_}} = delete $hash{$_} for keys %new;

Last modified on 2013-11-07