in reply to Efficiency: Foreach loop and multiple regexs
Are you trying to match only the items in @array that match all of your regexes? If so, then I would probably write the loops with the outer one as foreach @regex (since @regex is much smaller) .. Please correct me if I misinterpreted your question.
Something like this might improve your efficiency -- as soon as an item fails a test, it is removed from @array, so we have less and less to loop through each time. Ideally you would want to organize @regex so that the most restrictive expressions (those that fail on the highest number of items in @array) come first -- that would give you the best efficiency.foreach $reg (@regex) { my $i = 0; while ($i <= $#array) { if ($array[$i] =~ /$x/) { # successful, so keep $array[$i] in array and move to the +next $i++; } else { # this item didn't pass so we remove it for good and # never have to test it again. splice(@array, $i, 1); } } }
Let me know if this is not what you intended for this code snippet.
blokhead
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Efficiency: Foreach loop and multiple regexs
by neilwatson (Priest) on Sep 13, 2002 at 14:47 UTC | |
by blokhead (Monsignor) on Sep 13, 2002 at 14:53 UTC | |
by neilwatson (Priest) on Sep 14, 2002 at 16:39 UTC |