foreach my $key (keys %a) { $a{$key} = $b{$key} if exists $b{$key}; }