in reply to Re: Re: Efficiency: Foreach loop and multiple regexs
in thread Efficiency: Foreach loop and multiple regexs

OK, in that case, we can kinda swap logic in the if-statement.
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++; } } }
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.

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
    Problem with splice in this case. See here.

    Update Ack! nevermind I understand your clever use of else now.

    Neil Watson
    watson-wilson.ca