in reply to array filter

So, yo are studying computer linguistics? ; )
-> searching in array elements substrings contained in another array

Ikegami's solution to build up a big filter-regex might be faster, but maybe this one is just simpler

@main=("In to the wild" ,"The catcher in the rye") ; @filters=("the","in"); for $filter (@filters) { s/\Q$filter\E//ig for @main; } print "@main"; # to wild catcher rye
(tested in perlshell)

\Q and \E are for quoting possible regexcode in @filter. If you want to get rid of whitespaces or dupplicate replacements and so on just improve the regex, it's up to you!

Cheers LanX

- - - - - Which song???

Replies are listed 'Best First'.
Re^2: array filter
by Anonymous Monk on Nov 18, 2008 at 11:34 UTC
    Thanks to all. I tried to use the suggested solutions, but it seems I have not well specified what I meant: the words included in @filter must match when they're not embedded in other words... Using the code above, when @filter meets a phrase in @main that contains, for example, "Ethernet", it strips out "the" and it returns "Ernet" , and that's not what I meant... Sorry if I was unclear.. I tried anyway to do :
    for $filter (@filters) { s/\Q\b$filter\b\E//ig for @main; }
    adding the \b bound delimiter, but still it doesn't work. How should I improve the regex to obtain what I need? Thank you again.
      \b should be OK, but your escaping it within \Q and \E !!!

      Cheers LanX

      - - - - - Which song???

        ooops... Sorry, stupid me. Now it works! Thanks again for the help and... God bless the Monastery and all the Monks.
        oops... Stupid me! Sorry... Thanks again for all your kind help. God bless the Monastery and all the Monks.
      > .. Sorry if I was unclear.. I

      no prob, a certain unclearity was obvious and the reason why I gave you a simple and expandable approach to start with. 8 )

      So, did you already think about the whitespaces left? ; )

      You may want to consider \s* instead of \b.

      When speed is a matter think about tuning the final solution like ikegami showed you!

      Cheers LanX

      - - - - - Which song???