foreach my $entry (@file1) { $index{$entry} = 1 } foreach my $entry (@file2) { # '||0' is to still the warnings under 'use warnings' $index{$entry} += 2 if (($index{$entry}||0) < 2); }