foreach $k (sort keys %hash1) { if (exists($hash2{$k})) { $matchWords++; } }