develooper Front page | perl.beginners | Postings from March 2002

precompile regular expressions?

Thread Next
Nikola Janceski
March 21, 2002 10:49
precompile regular expressions?
Message ID:
Is there a way to precompile regular expressions?

I have an array of acceptable matches, and an array of items to grep from.
I want to avoid recompiling the regular expression over and over again, for
each loop.

ie. (imagine these lists are much longer).

@stuff = (
@accept = qw( Ja Ma );

my @goodstuff;
foreach my $item (@stuff){
	foreach my $accept (@accept){
		push @goodstuff, $accept if $item =~ /$accept/;  ## my
problem is this might take long recompiling each time

print "@goodstuff\n";


# alternative is:
foreach my $accept (@accept){
	push @goodstuff, grep /$accept/, @stuff;
# but this isn't better because in my script @accept is much bigger/longer
than @stuff

Nikola Janceski

Here it is my time, 
Where there are no crimes, 
Only I exist here, 
And have no fear.
-- Riddles

The views and opinions expressed in this email message are the sender's
own, and do not necessarily represent the views and opinions of Summit
Systems Inc.

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About