in reply to how can i replace a hash key with other hash value?

A quick example to show the method required
use strict; use warnings; my %hash=("one" =>1, "two"=>,2); my %hash_tr_fr=(one=>"une","two"=>"deux"); for my $eng (keys %hash){ $hash{$hash_tr_fr{$eng}}=$hash{$eng}; delete $hash{$eng}; } use Data::Dumper; print Dumper(\%hash);
You can't "replace a key" but you can set a new one and delete the existing one

print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."

Replies are listed 'Best First'.
Re^2: how can i replace a hash key with other hash value?
by JavaFan (Canon) on Mar 24, 2010 at 10:39 UTC
    You can't "replace a key" but you can set a new one and delete the existing one
    And you can do that in one line:
    $hash{$hash_tr_fr{$eng}} = delete $hash{$eng};
    which has the added benefit to even work correctly when $hash_tr_fr{$eng} eq $eng. Doing the delete in a separate step would result in a loss of the entry.
      ...or even in a slice without the loop. So:
      for my $eng (keys %hash){ $hash{$hash_tr_fr{$eng}}=$hash{$eng}; delete $hash{$eng}; }
      becomes:
      my @temp = keys %hash; @hash{@hash_tr_fr{@temp}} = delete @hash{@temp};
      Update: corrected missing @, thanks SuicideJunkie