Do you mean:
my ($c,@compiled) = ();
for(@accept) {
$compiled[$c++] = qr/$_/;
}
? then you can loop over @compiled instead of @accept, and it should be
quite a bit faster. It sounds like that's what you're looking for. There
is a big section on compiling regular expressions in O'Reilly
Programming Perl, 3rd edition, if you have that available.
-dave
> -----Original Message-----
> From: Nikola Janceski [mailto:[EMAIL PROTECTED]]
> Sent: Thursday, March 21, 2002 1:50 PM
> To: Beginners (E-mail)
> Subject: precompile regular expressions?
>
>
> 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.
>
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]