in reply to I want to operate on some values in a hash

Hello misterperl,

Why not simply rename the key on the flow?

Sample of code:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %hash = ( 'key1' => 'test1', 'key2' => 'test2', 'key3' => 'test2', ); print Dumper \%hash; $hash{'key4'} = delete $hash{'key1'}; print Dumper \%hash; __END__ $ perl test.pl $VAR1 = { 'key1' => 'test1', 'key3' => 'test2', 'key2' => 'test2' }; $VAR1 = { 'key2' => 'test2', 'key3' => 'test2', 'key4' => 'test1' };

Update: For multiple keys you can use hash slice:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %hash = ( 'key1' => 'test1', 'key2' => 'test2', 'key3' => 'test2', 'key4' => 'test4', ); print Dumper \%hash; my @oldKeys = sort keys %hash; print Dumper \@oldKeys; # Remove what ever keys you want to keep e.g. key1 key3 splice @oldKeys, 0, 1; splice @oldKeys, 1, 1; print Dumper \@oldKeys; my @newKeys = ('key5', 'key6'); @hash{@newKeys} = delete @hash{@oldKeys}; print Dumper \%hash; __END__ $ perl test.pl $VAR1 = { 'key2' => 'test2', 'key3' => 'test2', 'key1' => 'test1', 'key4' => 'test4' }; $VAR1 = [ 'key1', 'key2', 'key3', 'key4' ]; $VAR1 = [ 'key2', 'key4' ]; $VAR1 = { 'key6' => 'test4', 'key5' => 'test2', 'key1' => 'test1', 'key3' => 'test2' };

Update2: Update the keys with for loop:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %hash = ( 'key1' => 'test1', 'key2' => 'test2', 'key3' => 'test3', ); print Dumper \%hash; my @new_keys = keys %hash; y/key[0-9]/alt[0-9]/ for @new_keys; @hash{@new_keys} = delete @hash{keys %hash}; print Dumper \%hash; __END__ $ perl test.pl $VAR1 = { 'key1' => 'test1', 'key2' => 'test2', 'key3' => 'test3' }; $VAR1 = { 'alt3' => 'test3', 'alt2' => 'test2', 'alt1' => 'test1' };

Update3: Also with map (I think this is slower):

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %hash = ( 'key1' => 'test1', 'key2' => 'test2', 'key3' => 'test3', ); print Dumper \%hash; %hash = map { y/key[0-9]/alt[0-9]/r => $hash{$_} } keys(%hash); print Dumper \%hash; __END__ $ perl test.pl $VAR1 = { 'key2' => 'test2', 'key1' => 'test1', 'key3' => 'test3' }; $VAR1 = { 'alt3' => 'test3', 'alt2' => 'test2', 'alt1' => 'test1' };

Hope this helps, BR.

Seeking for Perl wisdom...on the process of learning...not there...yet!