From: Peng Yu on
If I only have a small number of patterns(say 3), I can just spell out
the matching code as below.

grep {/$pattern1$/ or /$pattern2$/ or /$pattern3$/} @array;

But if I have @patterns with many patterns that I want grep, the above
way doesn't work. I'm wondering what is the best way to grep many
patterns.
From: Willem on
Peng Yu wrote:
) If I only have a small number of patterns(say 3), I can just spell out
) the matching code as below.
)
) grep {/$pattern1$/ or /$pattern2$/ or /$pattern3$/} @array;
)
) But if I have @patterns with many patterns that I want grep, the above
) way doesn't work. I'm wondering what is the best way to grep many
) patterns.

That depends on a number of things, such as how many items and times you
will be matching with the same set of patterns, how complex these patterns
are, and if you value maintainability over execution speed.

Possibilities I can think of offhand:
- Nested greps (two possible ways to nest)
- Nested greps with precompiled regexes
- Combining search patterns into one big regex

And there are probably more.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
From: Peng Yu on
On Jun 13, 9:46 am, Willem <wil...(a)turtle.stack.nl> wrote:
> Peng Yu wrote:
>
> ) If I only have a small number of patterns(say 3), I can just spell out
> ) the matching code as below.
> )
> ) grep {/$pattern1$/ or /$pattern2$/  or /$pattern3$/} @array;
> )
> ) But if I have @patterns with many patterns that I want grep, the above
> ) way doesn't work. I'm wondering what is the best way to grep many
> ) patterns.
>
> That depends on a number of things, such as how many items and times you
> will be matching with the same set of patterns, how complex these patterns
> are, and if you value maintainability over execution speed.

Let's say I have only 10 simple patterns (just have non-special
characters, a-z, A-Z, _, and \.) and they are mutually exclusive (if a
file match one pattern it can not match another).

> Possibilities I can think of offhand:
> - Nested greps (two possible ways to nest)

It seems that the above one is the simplest solution for this
particular problem. Would you pleas show me some code on how to use
nested greps?

> - Nested greps with precompiled regexes
> - Combining search patterns into one big regex
>
> And there are probably more.
>
> SaSW, Willem
> --
> Disclaimer: I am in no way responsible for any of the statements
>             made in the above text. For all I know I might be
>             drugged or something..
>             No I'm not paranoid. You all think I'm paranoid, don't you !
> #EOT

From: Willem on
Peng Yu wrote:
) On Jun 13, 9:46?am, Willem <wil...(a)turtle.stack.nl> wrote:
)> That depends on a number of things, such as how many items and times you
)> will be matching with the same set of patterns, how complex these patterns
)> are, and if you value maintainability over execution speed.
)
) Let's say I have only 10 simple patterns (just have non-special
) characters, a-z, A-Z, _, and \.) and they are mutually exclusive (if a
) file match one pattern it can not match another).

In that case you could probably join them into one regex quite easily:

my $patt = join('|', @patterns);
grep { /$patt/ } @array;

)> Possibilities I can think of offhand:
)> - Nested greps (two possible ways to nest)
)
) It seems that the above one is the simplest solution for this
) particular problem. Would you pleas show me some code on how to use
) nested greps?

grep { my $x = $_; grep { $x =~ /$_/ } @patterns } @array;

)> - Nested greps with precompiled regexes
)> - Combining search patterns into one big regex
)>
)> And there are probably more.

SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
From: Tad McClellan on
Peng Yu <pengyu.ut(a)gmail.com> wrote:
> If I only have a small number of patterns(say 3), I can just spell out
> the matching code as below.
>
> grep {/$pattern1$/ or /$pattern2$/ or /$pattern3$/} @array;
>
> But if I have @patterns with many patterns that I want grep, the above
^^^^
> way doesn't work. I'm wondering what is the best way to grep many
> patterns.


Please refrain from re-asking Frequently Asked Questions, it
is getting tiresome...


perldoc -q many

How do I efficiently match many regular expressions at once?


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.