Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: Dynamic regexp from array values

by DaveH (Monk)
on Feb 11, 2003 at 21:07 UTC ( #234530=note: print w/replies, xml ) Need Help??

in reply to Dynamic regexp from array values


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;". :-)


-- Dave :-)


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://234530]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2023-03-23 17:10 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (60 votes). Check out past polls.