in reply to Re: array substr sort ??
in thread array substr sort ??

As written, what you have is really slow. This is because sort has to call the subroutine you pass it dozens of times even for really small sets. Fortunately there is an easy and fast way to optimize it, called the schwartzian transform. It would look like this:
@array = map { $_->[1] } sort { $a->[0] cmp $b->[0] } map { [lc substr($_,18),$_] } @array;
What this does is first create a list of arrays, each array contains two elements, the first one is your munged data (lc,substr), and the second element is the actual data. Then you call sort and tell it to compare just the first element, that is, your munged data, then you map again to retreive just the second elements, your actual data.

Replies are listed 'Best First'.
Re: Re: Re: array substr sort ??
by japhy (Canon) on Nov 07, 2003 at 22:09 UTC
    I would change it slightly to let me use the native sort:
    @array = map substr($_, 1 + index($_, "\n")), sort map lc($_) . "\n$_", @array;
    But I admit I have not done benchmarks.

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;