open( LIST, "mystopwords.txt" ) or die "$!"; my @stopwords = ; # assuming one stop word per line close LIST; chomp @stopwords; my $stopregex = join '|', @stopwords; # ... now, when you go to delete stopwords from $_, # it goes like this: s/\b(?:$stopregex)\b//g; #### my %stopwd; open( LIST, "my_stopwords.txt" ) or die "$!"; while () { chomp; $stopwd{$_} = undef; # assume one word per line } close LIST; # now, to remove stopwords, split the input data ($_) on \b # and check each token: my $filtered = join '', map { exists($stopwd{$_}) ? '':$_ } split /\b/;