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

I should have been more clear. I'm tyring to match ANY item in @array that matches any regex in @regex.

Neil Watson
watson-wilson.ca

  • Comment on Re: Re: Efficiency: Foreach loop and multiple regexs

Replies are listed 'Best First'.
Re: Re: Re: Efficiency: Foreach loop and multiple regexs
by blokhead (Monsignor) on Sep 13, 2002 at 14:53 UTC
    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

      Problem with splice in this case. See here.

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

      Neil Watson
      watson-wilson.ca