On 08/22/2012 10:34 PM, Eduardo wrote:
> On 22/08/12 03:49, Uri Guttman wrote:
>> On 08/21/2012 08:29 PM, Eduardo wrote:
>>> how would you do with Sort::Maker?
>> i don't have time to show an example now but it is much cleaner
>> looking. all you need to do is code up how you extract each key from
>> the data set and how it gets sorted (number vs string, etc.). it makes
>> sorting into a declarative problem instead of a coding problem.
>>
>> uri
> 
> do'nt worry, that's what I'm
> 
> Thanks a lot for forcing me to study sorting in perl.
> 
> This morning I read sort_paper[*] apparently written by someone expert
> with the subject, and also Chapter 2 item 22 Effective perl, to get an
> overview.
> 
> After that I have a vague idea and got this code:
> 
> my @sorted = map  { $_->[0] }
>              sort { $a->[1] <=> $b->[1] }
>              map  { [ $_, /^(\d+)-(\d+)/ && ($1 * 100 + $2) ] }
>              keys %$hash;
> 
> Tonight I reread the Sort::Maker manual, now I understand and I was able
> to create this code:
> 
> my $sorted = make_sorter( name => 'sort_func',
>                           orcish => 1,
>                           number => '/^(\d+)-(\d+)/ && ($1 * 100 + $2)'
> that generate this:
> 
> sub {
>    
>     my ( %or_cache1 ) ;
> 
>    
>     sort {
>     (
>       ( $or_cache1{$a} ||=
>         do{ my ($val) = map { /^(\d+)-(\d+)/ && ($1 * 100 + $2) } $a ;
> $val } )
>             <=>
>       ( $or_cache1{$b} ||=
>         do{ my ($val) = map { /^(\d+)-(\d+)/ && ($1 * 100 + $2) } $b ;
> $val } )
>     )
> 
>     } @_  ;
> }
> 
> 
> I did not think that I obtained so high performance in a short time, I
> just subscribe to the list.
> 
> It's a pity Sort::Maker not in Debian

There is also Sort::Key, available in Debian testing and unstable, and
which is usually faster than Sort::Maker and also Sort::Key::Radix, even
faster when sorting by numeric keys but not available in Debian.

  use Sort::Key qw(ukeysort);

  my @sorted = ukeysort { /^(\d+)-(\d+)/
                            or die "bad key $_";
                          $1 * 100 + $2 } @data;


The 'u' prefix in 'ukeysort' specifies that the sorting key is an
unsigned integer.



-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to