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