in reply to grep for array-of-strings from an array-of-strings

You could use join to build your array into a single regex with or's, and then just do a single match against that.
  • Comment on Re: grep for array-of-strings from an array-of-strings

Replies are listed 'Best First'.
Re: Re: grep for array-of-strings from an array-of-strings
by nothingmuch (Priest) on Apr 09, 2003 at 19:25 UTC
    That'll match "this is a test". It needs to also contain 'another'. It's an AND boolean, not an OR boolean search...

    -nuffin
    zz zZ Z Z #!perl
      Oh.. if you want an AND, perhaps the look-ahead operator is what you want? I think it shouldn't be too hard to programmatically construct a regex with that style either..
Re: Re: grep for array-of-strings from an array-of-strings
by zby (Vicar) on Apr 09, 2003 at 19:22 UTC
    And here is some code for it: my $criteria = join "|", @searchCriteria; and then use it in the regex like that: /$criteria/

    Update: I realized that the 'and' case is much more difficult that the 'or' case that I presented. I would use eval for it:  my $criteria = join "/ and /", @searchCriteria; $criteria = "/$criteria/"; and then use it like this grep {eval $criteria}. Which is just your second code dynamically generated.