Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Golf: Arbitrary Alphabetical Sorting

by tadman (Prior)
on May 10, 2001 at 03:09 UTC ( #79285=note: print w/replies, xml ) Need Help??


in reply to Golf: Arbitrary Alphabetical Sorting

Taking a few tricks from tilly, and influences from merlyn, this one appears to be 74 characters:
sub o { ($c,*w,$")=@_;sub t{eval"y/\Q@$c\E/\0-\377/";$_}sort{t($_=$a)cmp t$_=$ +b}@w }
This will only work on single character 'alphabets', or those that y// can handle as UNICODE.

Update:
I forgot to include the "\Q" and "\E" escapes to prevent it from going south if the "alphabet" contained characters such as '/' or '\n'. 4 character penalty.

Update 2:
If you want to cheat, you can always insert the literal character ASCII 255 into the string instead of the comparatively verbose '\377', which saves 3 characters, yielding a length of 71, though some editors show that single "character" as 2 on screen.

Replies are listed 'Best First'.
Re (tilly) 2: Golf: Arbitrary Alphabetical Sorting
by tilly (Archbishop) on May 10, 2001 at 12:02 UTC
    Excellent. As I responded in the buried thread, you can insert the literal characters for both escape sequences, yielding 70. This may really fubar some editors, but perl can handle it.

    BTW FWIW I don't consider this cheating.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://79285]
help
Chatterbox?
[karlgoethebier]: Lady_Aleena: "Studying the sources is the way to go" (Dr Wisenheimer) ;-)
[Lady_Aleena]: karlgoethebier, when I don't know what I am looking at, it makes it hard to figure things out.

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2017-06-26 09:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (575 votes). Check out past polls.