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

Re: [FWP] sifting



On Wed, Apr 12, 2000 at 12:47:32PM -0400, Jeff Pinyan wrote:
> On Apr 12, John Porter said:
> 
> >print
> >  map { substr($_,1) }
> >  sort
> >  map {
> >    my $k = 4;
> >    /gnome/   and $k = 2;
> >    /session/ and $k--;
> >    $k.$_
> >  }
> >  <DATA>;
> 
> That sorts the data within itself -- a possibly unwanted side effect.  But
> I like the GR transform.  It would need to be scalable though.

I was working on the same idea, but my solution preserves order and is
scalable:

sub sift {

    my @list = @{ shift(@_) };
    my @re = @_;

    my $list_l = length @list;
    my $re_l = length @re;

    foreach (@list) {
        for ($i = 0; $i <= $#re; ++$i) {
            last if /$re[$i]/;
        }
        $_ = sprintf "%0${re_l}d%0${list_l}d%s", $i, $c[$i]++ || 0, $_;
    }
    map { substr($_, $re_l + $list_l) } sort @list;

}


As you might expect, it's also somewhat slower than the other solutions.

Ronald

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