my @words = get_words_to_check(); my %hash = map { $_ => 1 } @words; while (my $line = <>) { chomp $line; delete $hash{$line} if exists $hash{$line}; # The if exists isn't required here, but it does make it look cleaner } my @good_words = grep { exists $hash{$_} } @words; # Keep the original order my @good_words_2 = keys %hash; # Don't care about the original order #### my $index = 0; LINE: while (my $line = <>) { chomp $line; # While the word in the dictionary is past (or equal) to the word to check while ($line ge $words[$index]) { # Store the word as OK, unless it is equal to the current dict word push @good_words, $words[$index] unless $line eq $words[$index]; # Use the next word from the list of words to check, if any last LINE if ++$index == @words; } }