in reply to Hash substitution

You can't change the key of a hash because the key decides how to find the data based on an algorithm.

You must instead move the value, easiest done this way:

$hash{newkey} = delete $hash{oldkey};
The delete removes the value from the old hash location and returns the value, allowing you to put it in the new hash location.

                - Ant
                - Some of my best work - (1 2 3)

Replies are listed 'Best First'.
Re^2: Hash substitution
by Narveson (Chaplain) on Oct 09, 2008 at 12:49 UTC
    use Data::Dumper; my %hash = ( apple => 'red', orange => 'orange', banana => 'yellow', ); my $old_key = 'orange'; my $new_key = 'carrot'; $hash{$new_key} = delete $hash{$old_key}; print Dumper(\%hash);