Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Deleting values from string using array

by aaron_baugher (Curate)
on Jun 16, 2012 at 11:40 UTC ( [id://976571]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://976571]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (1)
As of 2024-04-25 19:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found