On 16/06/2003 20:54:52 Michael Higgins wrote:

>FARRINGTON, RYAN wrote:
>> I think I bugged Bill enough so I'll ask the list...
>>
>> This was provided by Bill:
>>
>>         my @sortlist = map { $_->[0] }
>>                        sort { mysort ($field, $type, $case, $dir) }
>>                        map { [ $_, split /\|/ ] } @lista;
>>
>>
>> am I reading this correct:
>> map returns an array for each value in @lista
>> we then sort each value in this anonumous array according to our
>> specifications
>> what does map{$_->[0] } do?
>
>Ryan --
>
>Basically, reading right to left, you're putting the value of $_ and the
>values of splitting $_ from @lista into an anonymous array, sorting via
>'mysort', then de-referencing the first value (the original value of the
>array slice) from that anonymous array you created (as you are still
>mapping against $_ from @lista.
>

This technique is known as the "Schwartzian transform".
See "How do I sort an array by (anything)" in

perldoc -q sort

Basically it caches the result of split via the "bottom" map(),
sorts on the cached values, then it throws away the cached values
with the "top" map().

The simplistic approach would be to split inside the sort() as needed.
Because each element needs to be compared more than once, this would
result in repeated split() calls for the same element. Instead,
the bottom map ensures that split() is called only once for each
element and the result is ready to be used by sort().

--
Csaba Ráduly, Software Engineer, Sophos Anti-Virus
Email: [EMAIL PROTECTED], Tel: 01235 559933, Web: www.sophos.com
Add live virus info to your website: http://www.sophos.com/link/vfeed


_______________________________________________
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to