in reply to Dynamic regexp from array values
Hi.
A similar discussion was had a few months ago here: Set Operators. Everyone seemed to come up with different answers, but like so much other stuff with Perl, "it depends" was the best answer.
If you can afford the memory, and your list of values to test against is reasonably small, building a hash slice is a nice "Perlish" solution. It can also pay long-term if you are going to be doing these searches over and over again.
On the other hand, if you only have a few values to test against, hashes may be more trouble than they're worth - just use a foreach loop. An example of where this would be perfectly appropriate would be parsing commandline arguments manually (although you should generally use some sort of Getopt).
However, since it is implemented directly in C as a core function, you would have to work hard to beat grep in terms of raw speed. At the same time, you are also guaranteed that grep will examine every item of the search list, every time. You get no chance to `last' out of the loop early if your needs have already been matched. Raw speed only scales so far...
Unfortunately, the old adage still stands - know your data. Only you can decide which approach works best; and if in doubt - "use Benchmark;". :-)
Cheers,
-- Dave :-)
$q=[split+qr,,,q,~swmi,.$,],+s.$.Em~w^,,.,s,.,$&&$$q[pos],eg,print