http://www.perlmonks.org?node_id=157735


in reply to Union of Two Hashes

The question is a bit confusing...you ask for a union, but you code snippet looks like you're looking for the intersection. Union first. For small hashes this is really quick:
%union = (%first, %second);
For large:
while (($k,$v) = each %first) { $union{$k} = $v; } while (($k,$v) = each %second) { $union{$k} = $v; }
(makes sense once you consider the memory usage for the first snippet.

For intersection, all you really need to do is change $matchWords to $matchWords{$k}++ the iterate of then matchWords looking for values == 2