[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

RE: [FWP] sifting



On Apr 12, Larry Rosler said:

>This should be faster for your loop, as it avoids copying the remaining
>list on each pass:
>
>  for my $re (@_) {
>    for (my $i = 0; $i < @list; ++$i) {
>        $list[$i] =~ /$re/ and push @sifted, splice @list, $i--, 1;
>    }
>  }

No, it slowed it down.  I got really fancy with my grepper now:

  sub grep_sift {
    my @sifted = ();
    my @list = @{ shift() };

    for my $re (@_) {
      my $regex = ref $re eq "Regexp" ? $re : qr/$re/;
      my ($i,$j) = (0,0);  
      push @sifted, grep +(
        $j++, $_ =~ $regex ? (splice(@list,$j-1-$i,1), ++$i) : 0
      ), @list;
    }
   
    return (@sifted,@list);
  }

splice()ing does help speed it up.  Still testing.

-- 
MIDN 4/C PINYAN, NROTCURPI, US Naval Reserve             japhy@pobox.com
http://www.pobox.com/~japhy/                  http://pinyaj.stu.rpi.edu/
PerlMonth - An Online Perl Magazine            http://www.perlmonth.com/
The Perl Archive - Articles, Forums, etc.    http://www.perlarchive.com/


==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe