in reply to Deleting values from string using array

PerlMonks taught me about Regexp::Assemble, an excellent module for building regex patterns from lists of strings.

#!/usr/bin/env perl use Modern::Perl; use Regexp::Assemble; my @to_remove = qw( is an to for from ); my @strings = ( 'is that true', 'this is from presentation', 'to create', ); my $re = Regexp::Assemble->new->add(@to_remove); # say $re; # uncomment to see the regex it makes map { s/$re//g } @strings; # remove strings anywhere # map { s/\b$re\b//g } @strings; # or just remove whole words say for @strings;

Aaron B.
Available for small or large Perl jobs; see my home node.

Replies are listed 'Best First'.
Re^2: Deleting values from string using array
by Cristoforo (Curate) on Jun 16, 2012 at 18:49 UTC
    Just a small note. Starting with perl 5.010, you can assemble the stop words into a alternating string separated by the pipe, '|', character and it will use a 'trie' to combine the stop words. It is claimed (by somebody?), the 'trie' approach is big-O 1, an algorithm in constant time (in most cases, they claim). Similiar to what Regexp::Assemble does.

    I think I read somewhere that you shouldn't use Regexp::Assemble with perl version > 5.8 because it will muck things up.