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

precompile regular expressions?

Thread Next
From:
Nikola Janceski
Date:
March 21, 2002 10:49
Subject:
precompile regular expressions?
Message ID:
1449413DA482D311B67000508B5A12F50592DF1E@nyexchange01.summithq.com
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 = (
	"Micheal",
	"Marko",
	"Marcy",
	"Rebecca",
	"Bob",
	"Jancy",
	"Jill",
	"Jamie",
	"Jack",
	);
@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";

__END__

# 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


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About