Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Faster than the speed of sound (was: When the Best Solution Isn't)

by blakem (Monsignor)
on Sep 23, 2002 at 17:58 UTC ( #200186=note: print w/replies, xml ) Need Help??


in reply to Faster than the speed of sound (was: When the Best Solution Isn't)
in thread When the Best Solution Isn't

That looks a lot like a method used in Effective Perl Programming (see item 14 in the idiomatic perl pdf)

The goal is to sort /etc/passwd by the third field:

open PASSWD, "/etc/passwd" or die; @passwd = <PASSWD>; @key = map # Create an array @key contain +ing { (split /:/)[2] } @passwd; # just the keys (user ids). @by_uid_index = sort { # Sort indices, using @key arr +ay. $key[$a] <=> $key[$b] } 0 .. $#key; @by_uid = @passwd[@by_uid_index]; # Now, rearrange the contents +of # @passwd into sorted order. Or just combine the last two statements: @by_uid = @passwd[sort { $key[$a] <=> $key[$b] } 0 .. $#key];
Well, this will work, and it's efficient enough, but there are prettier, more Perl-ish ways to present the solution.
It then goes on to describe the Orcish and Schwartzian under the heading of 'Advanced sorting: the cool ways'

-Blake

  • Comment on Re: Faster than the speed of sound (was: When the Best Solution Isn't)
  • Download Code

Replies are listed 'Best First'.
Re^2: Faster than the speed of sound
by Aristotle (Chancellor) on Sep 23, 2002 at 18:04 UTC
    Interesting. It's more verbose with the extra array, and the Schwartzian Transform is such a common idiom one should definitely know it, but I wonder if the author(s) had any particular motive to recommend the ST over this meme?

    Makeshifts last the longest.

      Perhaps because the ST is named after the books co-author? ;-)

      Actually, that unnamed sorting method sits right between "sorting: the mundane way" and "sorting: the cool way". Guess they just liked the Orcish and ST better.

      -Blake

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://200186]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2019-11-12 03:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Strict and warnings: which comes first?



    Results (64 votes). Check out past polls.

    Notices?