Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: Golf: Arbitrary Alphabetical Sorting

by chipmunk (Parson)
on May 09, 2001 at 20:49 UTC ( #79156=note: print w/ replies, xml ) Need Help??

in reply to Golf: Arbitrary Alphabetical Sorting

This solution uses the same approach as merlyn's, translating the words so they can be compared directly. It assumes that the words will not contain the null character.

96 characters.

sub o { @k{@$A}=map chr,1..@{$A=$_[0]}; map{s/.*\0//s;$_}sort map{($x=$_)=~s/./$k{$&}/gs;"$x\0$_"}@{+pop} }
BTW, although %k is not lexicalized to the sub, I don't think it matters, since any leftover contents which are not overwritten won't be used. (Also assume that no other characters are present in the words.)

Update: I added /s to both substitutions, to avoid problems with \n. Thanks tilly!

Comment on Re: Golf: Arbitrary Alphabetical Sorting
Download Code
Replies are listed 'Best First'.
Re (tilly) 1: Golf: Arbitrary Alphabetical Sorting
by tilly (Archbishop) on May 09, 2001 at 21:36 UTC
    Note that in addition to not allowing \0 as a character you don't allow \n.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (10)
As of 2015-11-25 00:40 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (666 votes), past polls