Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: (Golf) Keysort

by chipmunk (Parson)
on Feb 18, 2002 at 05:51 UTC ( #146105=note: print w/ replies, xml ) Need Help??


in reply to (Golf) Keysort

Here's my lexicographic solution, which comes in at 89 characters (including the call to sort):

sub chipmunk_lexi { sort{(($B)=$b=~($r='^\s*(0|-?[1-9]\d{0,8})(?!\d)'))<=>(($A)=$a=~$r)||$ +A<=>$B||$a cmp$b}@_ }
If I've understood the hole properly, the definitions of "integer" and "starting with an integer" are a bit tricky. '0' is an integer, but '00' isn't; '0a' starts with an integer, but '00a' doesn't. Thus, '0', '1', '00', 'adam' would be sorted in that order. Have I got that right?


Comment on Re: (Golf) Keysort
Download Code
Re: Re: (Golf) Keysort
by demerphq (Chancellor) on Feb 18, 2002 at 15:59 UTC
    Nice!

    And you are correct with how im defining an integer.

    However you are correct that there is a minor problem with the regex. The ignore leading whitespace shouldnt happen, as it prevents reconstructive methods from being successful. (Although ST style stuff should be fine with it...)

    So the regex should be

    /^(0|-?[1-9]\d{0,8})/
    Your (?!\d) is a worthy and intelligent addition, but I was willing to overlook the issue it resolves. ;-)

    BTW, my reasoning for disallowing 0000 and the like was to ensure that binary hash keys were sorted lexicographically.

    Im going to update the original node with your score and the bit about the regex tomorrow.

    Yves / DeMerphq
    --
    When to use Prototypes?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2014-12-26 18:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (174 votes), past polls