in reply to Re: Re: Efficiency: Foreach loop and multiple regexs
in thread Efficiency: Foreach loop and multiple regexs
Take successful matches, put them somewhere, and remove them since we don't need to ever check them again. In this scenario, you would want to put the regex which accepts the highest number of items first in @regex, so that more is removed from @array earlier.my @matches; foreach $reg (@regex) { my $i = 0; while ($i <= $#array) { if ($array[$i] =~ /$x/) { # successful, so this was a match! put it somewhere safe # also remove it from @array, we don't need to check it ag +ain push @matches, $array[$i]; splice(@array, $i, 1); } else { # this item didn't pass so keep it and try again next rege +x $i++; } } }
The replies from other monks also are good advice -- combining into one regex is a great idea as well.
blokhead
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Re: Efficiency: Foreach loop and multiple regexs
by neilwatson (Priest) on Sep 14, 2002 at 16:39 UTC |