in reply to Efficiency: Foreach loop and multiple regexs

After the grep how would I remove the entries in @bad_matches from @array before the next $x? Would this be faster?

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.

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); } } }
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.

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
    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

      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