foreach my $k (keys %hash1) { next unless exists $hash2{$hash1{$k}}; $hash1{$k} = $hash2{$hash1{$k}}; }