Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks

I'm matching strings (single words) contained in the array called @tokens against other strings (single words->stopwords) contained in the array called @stopords with the purpose of finding out if my strings (words) are present in the array, thus they are stopwords and I can discard them from the process. It is simple like that:

foreach my $token (@tokens) { if (grep /$token/i, @stopwords) { print "Stopword $token discarded\n"; } else { print "$token is okay\n"; } }

For some items in @tokens I get the following error message: An error occurred (Unmatched ) in regex; marked by <-- HERE in m/b) <-- HERE / at script.pm line 22

I guess it has to do with having meta characters in $token. But I don't know how to solve it in my example. Any suggestion will be appreciated

Replies are listed 'Best First'.
Re: grep unmatch error
by Corion (Patriarch) on Mar 05, 2016 at 11:44 UTC
Re: grep unmatch error
by NetWallah (Canon) on Mar 05, 2016 at 23:29 UTC
    If @tokens and @stopwords are matched on the complete word, you can perform the comparison much faster by converting @stopwords into a HASH.
    perl -E 'my @tokens=qw|this that other foo bar baz|; my %stopwords=qw|that 1 foo 1|; say for grep {!$stopwords{$_}} @tokens' __OUTPUT__ this other bar baz

            "Think of how stupid the average person is, and realize half of them are stupider than that." - George Carlin