Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re (tilly) 3: Golf: Arbitrary Alphabetical Sorting

by tilly (Archbishop)
on May 10, 2001 at 00:40 UTC ( #79243=note: print w/ replies, xml ) Need Help??


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

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.)


Comment on Re (tilly) 3: Golf: Arbitrary Alphabetical Sorting
Select or Download Code
Re: Re (tilly) 3: Golf: Arbitrary Alphabetical Sorting
by tadman (Prior) on May 10, 2001 at 01:39 UTC
    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.
Re^4: Golf: Arbitrary Alphabetical Sorting
by tadman (Prior) on May 10, 2001 at 03:04 UTC
    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 }
        I find this turn of events absolutely astounding, and I only have one thing to add.

        It may not display well, but the two escape sequences can be replaced by the actual characters. This brings it back down to 70. Here is the paste from my system, this may not work on yours:

        sub o{ ($c,*w,$")=@_;sub t{eval"y/\Q@$c\E/-/";$_}sort{t($_=$a)cmp t$_=$b}@w }

        UPDATE
        It definitely won't work on yours. It seems the \0 was trimmed from the output. To get what you need go back to chipmunk's post, download his code to a file, then run this:

        perl -pi.bak -e 's/\\(\d+)/chr(oct($1))/e' filename
        (Use " instead of ' on Windows.) The resulting file will have the key subroutine reduced to 70 characters, and will still run.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (9)
As of 2014-07-31 21:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (253 votes), past polls