Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Closest-value-in-list Golf!

by suaveant (Parson)
on May 29, 2001 at 17:57 UTC ( #83927=note: print w/replies, xml ) Need Help??


in reply to Closest-value-in-list Golf!

Why are you using strict in golf if it is not required?
sub g {$n=pop;$h{abs($_-$n)}=$_ for@_;@s=sort{$a<=>$b}keys%h;$h{shift@ +s};}
66 chars with strict off

Update

sub g {$n=pop;$h{abs($_-$n)}=$_ for@_;@s=sort{$a<=>$b}keys%h;$h{shift@ +s}}
oops trailing ; is extraneous... 65 strokes...

Update2 oooh!

sub g {$n=pop;$h{abs($_-$n)}=$_ for@_;$h{(sort{$a<=>$b}keys%h)[0]}}
59 strokes
                - Ant

Replies are listed 'Best First'.
(boo)Re: Re: Closest-value-in-list Golf -- or, a sorted affair.
by boo_radley (Parson) on May 29, 2001 at 18:38 UTC
    sort{$a<=>$b}keys%h
    is too much, since $a<=>$b is the default sort. that leaves you with
    sub g {$n=pop;$h{abs($_-$n)}=$_ for@_;$h{(sort keys%h)[0]}}
    for 52.
      did you test this? i think you'll find that $a cmp $b is the default.

      "Argument is futile - you will be ignorralated!"

      Test your code...

      that does not work... I tried it already... returns 20
                      - Ant

        How strange. I was using a different data set when I tested this, and it returned the correct answer.

        Mea culpa.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://83927]
help
Chatterbox?
[choroba]: I already gave a similar talk to my friends in a pub and at an internal conference at work and people liked it, so maybe...
[choroba]: LanX: That's the heritage, I can't do anything else
[RonW]: Sounds like some system my employer has "It does exactly what we need it to do and can't afford to risk anything we can't prove is 100% compatible"
[marto]: choroba sounds interesting
[RonW]: james28909 Why not write a Perl program to do the task?
[choroba]: RonW Yes, but then, one day, they needed to switch from FTP to SFTP, and... but I can't give the whole talk away here :)
[RonW]: Tunnel FTP through stunnel?

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (10)
As of 2017-05-22 21:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?