while ((my $a, my $b) = each %hash_1) { while ((my $c, my $d) = each %hash_2) { $b =~ s/$c/$d/g; } ... }