Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Deleting values from string using array

by Shaveta_Chawla (Acolyte)
on Jun 16, 2012 at 07:02 UTC ( #976544=perlquestion: print w/ replies, xml ) Need Help??
Shaveta_Chawla has asked for the wisdom of the Perl Monks concerning the following question:

I want to create a hash/array of some values and if the value from that hash/array matches with any value from string that should be removed. For Ex.
@array = ( is , an, to , for , from);

The multiple string are::
$str = "is that true"; $str1 = "this is from presentation"; $str2 = "to create";

Any value that matches from the array/hash should be deleted from the strings.

Comment on Deleting values from string using array
Select or Download Code
Re: Deleting values from string using array
by cheekuperl (Monk) on Jun 16, 2012 at 07:18 UTC
    @array = qw( is an to for from); @strs =("is that true","this is from presentation","to create"); print join("\n",@strs); print "\nTO DELETE: [@array]"; print "\n------PROCESS--------\n"; foreach $str (@strs) { @words=split(/\s+/,$str); print "\nWords in [$str] are [". join(",",@words)."]"; foreach $word (@words) { if(grep {$word eq $_} @array) { print "\n\tDeleting [$word] from [$str]"; $str=~s/\b$word\b//g; } } } print "\n--------OUTPUT---------\n"; print join("\n",@strs);
    =======================================
    is that true this is from presentation to create TO DELETE: [is an to for from] ------PROCESS-------- Words in [is that true] are [is,that,true] Deleting [is] from [is that true] Words in [this is from presentation] are [this,is,from,presentation] Deleting [is] from [this is from presentation] Deleting [from] from [this from presentation] Words in [to create] are [to,create] Deleting [to] from [to create] --------OUTPUT--------- that true this presentation create
      Shorter version
      @array = qw( is an to for from); @strs =("is that true","this is from presentation","to create"); print "\n---- INPUT ----\n"; print join("\n",@strs); $subs=join("|",@array); map{s/\b$subs\b//g} @strs; print"\n---- OUTPUT ----\n"; print join("\n",@strs);
Re: Deleting values from string using array
by aaron_baugher (Deacon) on Jun 16, 2012 at 11:40 UTC

    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.

      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.

Re: Deleting values from string using array
by Khen1950fx (Canon) on Jun 16, 2012 at 14:52 UTC
    I'd suggest that you start with one string at a time until you know more about what you are trying to do. List::AllUtils would be a good place to start. "delete" is trivial, so I leave that to you.
    #!/usr/bin/perl use autodie; use strict qw/refs/; use warnings FATAL => 'all'; use List::AllUtils qw/:all/; my (@arr) = qw( is an to for from ); my $str1 = ("This is not true"); print "At least one value is undefined\n" if all sub { defined $_ }, @arr; printf "\t %i item(s) is(are) defined ", true { defined $_ } $str1; print "\n"; print "\t The defined element is: before_incl { defined $_ } @arr"; print "\n";
Re: Deleting values from string using array
by Kenosis (Priest) on Jun 16, 2012 at 18:29 UTC

    Perhaps it's just a coincidence, but all the words in your array are stop words. Since you've already been given excellent examples of how to work with your specific variables, here's an option that removes stop words from the strings you've provided--in case that's your primary goal:

    use Modern::Perl; use Lingua::StopWords qw( getStopWords ); my $str = "is that true"; my $str1 = "this is from presentation"; my $str2 = "to create"; say removeStopWords( eval $_ ) for qw{$str $str1 $str2}; sub removeStopWords { my $stopWords = getStopWords('en'); join ' ', grep { !$stopWords->{$_} } split ' ', $_[0]; }

    Results:

    true presentation create

    Just include line 2 above in your code, and send a string to removeStopWords and it'll return it sans any stop words.

    Hope this helps!

Re: Deleting values from string using array
by kcott (Abbot) on Jun 17, 2012 at 08:17 UTC

    You can create a regular expression once and reuse it on each string.

    #!/usr/bin/env perl use strict; use warnings; my @del_words = qw{is an to for from}; my $del_words_alt = join q{|} => @del_words; my $del_re = qr{\b(?>$del_words_alt)\b\s*}; while (<>) { s{$del_re}{}g; print; }

    Test run:

    $ pm_string_array_del.pl is that true that true this is from presentation this presentation to create create

    -- Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://976544]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2014-11-28 12:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (196 votes), past polls