foreach (keys %firsthash) { if (exists($secondhash{$_})) { delete($firsthash{$_}; delete($secondhash{$_}; } }