Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

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!

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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2018-04-19 23:28 GMT
Find Nodes?
    Voting Booth?