while (my ($key, $val)= each %hash1) { exists $hash2{$val} and $hash1{$key}= $hash2{$val}; }