while ($line = ){ chomp $line; if (exists $hash2{$entry}) { print OUTPUT2 "$filename\n"; next; } # either exists in %hash1, # or not in either, # so output to OUTPUT1 print OUTPUT1 "$filename\n"; next; } close(MAIN_FILE1)