Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

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...
               s aamecha.s a..a\u$&owag.print

Replies are listed 'Best First'.
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 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.)

    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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://79184]
[marto]: good morning all

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2018-02-19 10:02 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (260 votes). Check out past polls.