Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Re (tilly) 1: Golf: Arbitrary Alphabetical Sorting

by MeowChow (Vicar)
on May 09, 2001 at 22:03 UTC ( #79184=note: print w/ replies, xml ) Need Help??


in reply to Re (tilly) 1: Golf: Arbitrary Alphabetical Sorting
in thread Golf: Arbitrary Alphabetical Sorting

Wow. I don't think this will work on a 99 character alphabet, but I'm too lazy to verify this :)

UPDATE 6: It just never ends:

sub o{ $c=join'',shift;sub t{$_=pop;s/./~0+index$c,$&/ges;$_}sort{t($a)cmp t$ +b}@{+pop} }
79 chars...
   MeowChow                                   
               s aamecha.s a..a\u$&owag.print


Comment on Re: Re (tilly) 1: Golf: Arbitrary Alphabetical Sorting
Download Code
Re (tilly) 3: Golf: Arbitrary Alphabetical Sorting
by tilly (Archbishop) on May 10, 2001 at 00:40 UTC
    First of all that doesn't work because shift returns a reference. You need it to dereference the first element into an array. Secondly you know I love a challenge..like 76 characters:
    sub o{ ($c,$s,$")=@_;sub t{$_=pop;s/./~0+index"@$c",$&/ges;$_}sort{t($a)cmp t +$b}@$s }
    (I have to waste 3 on removing spaces, dang.)

    UPDATE
    In 5.6.x it appears you need to spend another character:

    sub o{ ($c,$s,$")=@_;sub t{$_=pop;s/./1e9+index"@$c",$&/ges;$_}sort{t($a)cmp +t$b}@$s }

    UPDATE 2
    Saving another character..back to 76.

    sub o{ ($c,$s,$")=@_;sub t{s/./1e9+index"@$c",$&/ges;$_}sort{t($_=$a)cmp t$_= +$b}@$s }

    UPDATE 3
    Throwing in tadman's improvement, 75:

    sub o{ ($c,*w,$")=@_;sub t{s/./1e9+index"@$c",$&/ges;$_}sort{t($_=$a)cmp t$_= +$b}@w }
    (Note that chr returning 2 chars wouldn't make Unicode come out right. Hence I am keeping the 1e9 trick.)
      You can save a single character by using a glob, such as:
      sub o { ($l,*w,$")=@_;sub g{$_=pop;s/./chr index"@$l",$&/ges;$_}sort{g($a)cmp +g+$b}@w }
      So *w ... @w instead of $w ... @$w.

      '1e9' or 'chr' vary only in that '1e9' would support an alphabet of length > 255, but if you're using UNICODE, then chr would likely return two characters anyway.
      TMTOWTDI, as they say, and this one appears to be 70 characters:
      sub o { ($c,*w,$")=@_;sub t{eval"y/@$c/\0-\377/";$_}sort{t($_=$a)cmp t$_=$b}@w }
        Unfortunately, that fails if @$c includes a slash. Fixing this bumps it up to 74 characters:
        sub o { ($c,*w,$")=@_;sub t{eval"y/\Q@$c\E/\0-\377/";$_}sort{t($_=$a)cmp t +$_=$b}@w }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (10)
As of 2015-07-03 13:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (53 votes), past polls