"be consistent" | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
Using an joined pattern like "foo|bar|baz" can be quite inefficient depending on the patterns, since the regex engine can't find "anchored substrings" and can't guess where the match is. So here's some more or less attractive alternatives. The first one is the one I prefer if I don't know anything about the string nor patterns since it doesn't do more matches than necessary (comparing with the other two) and need it as an expression. It utilizes an "inline sub" as I call them, I don't know if there's any other name for them. It's a closure and why I use a subroutine for this is that I want to use it as an expression and be able to jump out of it. So &{sub { ... }} is like do { ... } except you can use return in it. (Note the subtle difference between &{sub { ... }} and sub { ... }->().) I admit it is a bit ugly, but at the same time I like it.
Of course, a less hacky way could be
which I prefer even more if I don't need it as an expression. This next one counts the matches, which is inefficient if you just want one match.
This one below does essentially the same, but is less memory hungry for big lists.
I'm not sure I really helped here... In reply to Re: matching elements in a list in a logical OR fashion
by ihb
|
|