Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Choosing the right sort (Or: WUDsamatter?)

by friedo (Prior)
on Apr 14, 2005 at 01:34 UTC ( #447633=perlmeditation: print w/replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
    @files = sort { my ($ad) = ( $a =~ /fwlog\.(\d+)\w+/ ); 
                    my ($bd) = ( $b =~ /fwlog\.(\d+)\w+/ ); 
                    $ad <=> $bd } @files;
    
  2. or download this
    foo1.tla
    foo10.tla
    ...
    foo101.tla
    foo102.tla
    etc.
    
  3. or download this
    sub naive {
        sort {
    ...
            $ma <=> $mb
        } @_;
    }
    
  4. or download this
    sub orcish {       
        my %c;
    ...
            $c{$a} <=> $c{$b};
        } @_;
    }
    
  5. or download this
    sub schwartzian {
        map { $_->[0] }
        sort { $a->[1] <=> $b->[1] }
        map { [ $_, /foo(\d+)\.tla/ ] } @_;
    }
    
  6. or download this
    sub guttros {  
        map { substr( $_, 3 ) }
        sort
        map { sprintf( "%03d", /foo(\d+)\.tla/ ) . $_ } @_;
    }
    
  7. or download this
    Benchmark: timing 5000 iterations of guttros, naive, orcish, 
    schwartzian...
    ...
      ( 0.04 usr +  0.00 sys =  0.04 CPU) @ 125000.00/s (n=5000)
     schwartzian: 26 wallclock secs 
      (25.70 usr +  0.03 sys = 25.73 CPU) @ 194.33/s (n=5000)
    

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://447633]
Approved by BrowserUk
help
Chatterbox?
[erix]: record separator
[Cosmic37]: Permissions are interesting earthlings. Did nature determine who gives permission and who asks permission. Who was the first to get permission? Are you not related to them as one big earthling family?
[karlgoethebier]: Cojones! We need cojones!
[karlgoethebier]: Ouch! Permissions! We need permissions!
[BarApp]: I can not use modules. I gain temporary access and still can not use modules.
[Cosmic37]: ta erix - this szabo geezer is pretty cool methinks and he writes about undef but I cannot see instructions for redefining the record separator after having undefined it
[Corion]: $/ = "wahtever";
[Corion]: (it's a magic variable)

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2017-06-29 16:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (672 votes). Check out past polls.