Re (tilly) 1: Closestvalueinlist Golf!
by tilly (Archbishop) on May 29, 2001 at 18:40 UTC

sub g{
(sort{abs($a$_[1])<=>abs$b$_[1]}@{$_[0]})[0]
}
 [reply] [d/l] 

how come you use ( ) on one abs but not the other? :)
sub g {(sort{abs$a$_[1]<=>abs$b$_[1]}@{$_[0]})[0]} # 44 chars
Update:
pop and no ref...
sub g {$n=pop;(sort{abs$a$n<=>abs$b$n}@_)[0]} # 39 chars
fantastic  that's exactly half my original
"Argument is futile  you will be ignorralated!"  [reply] [d/l] [select] 

sub g {
(sort{abs$a$_[0]<=>abs$b$_[0]}@_)[1]
}
 [reply] [d/l] 


Update my bad... sorry, missed the or listref bit
of golf, my response here was useless
 Ant
 [reply] 
Re: Closestvalueinlist Golf!
by chipmunk (Parson) on May 29, 2001 at 20:26 UTC

I disagree with tilly that using sort() is the natural solution. I think this is the natural solution:
sub closest {
$n=pop;$c=pop;abs$n$_<abs$n$c?$c=$_:0for@_;$c
}
This is O(n) rather than O(n*log n). Of course, at 47 characters it is, unfortunately, a longer solution. Oh well!  [reply] [d/l] 
Re: Closestvalueinlist Golf!
by jeroenes (Priest) on May 29, 2001 at 19:12 UTC

I can offer a 47 36 char solution with the use of PDL:
sub i{my$a=pdl$_[0];minimum_ind(abs($a$_[1]))}
and it is strictcompliant and very straightforward.
Of course it returns the index, but that is allowed,
isn't it? Otherwise, add 4 chars to get the value.
Hey wait, do you only count the chars between {}?
Than we can take of 7, that counts 40 chars. And there
is room in the last part:
sub i{my$a=pdl$_[0];minimum_ind abs$apop}
And we are at 36 chars.
Jeroen
"We are not alone"(FZ)  [reply] [d/l] [select] 

Why do you not count "use PDL"?
 [reply] 

 [reply] 
Re: Closestvalueinlist Golf!
by suaveant (Parson) on May 29, 2001 at 17:57 UTC

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  [reply] [d/l] [select] 

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.  [reply] [d/l] [select] 

 [reply] [d/l] 

 [reply] 


(boo) Re: Closestvalueinlist Golf!
by boo_radley (Parson) on May 29, 2001 at 17:58 UTC

oops :
Warning: Use of "keys" without parens is ambiguous at C:\trash.pl line 8.
my answer is forthcoming, will update.
update you can shave off a few chars by changing $h{shift@s}
to
$h{$s[0]}
.
Don't get too complex, eh?
UpdateTime's up!
sub e {sub n{abs($s$b)};$s=pop;while($b=pop){if(&n<$d!$d){$l=$b;$d=
+&n}}$l}
for a total of 70 chars.
 [reply] [d/l] [select] 

even shorter to not assign to @s at all, like my Update2
 Ant
 [reply] 

 [reply] [d/l] 

Re: Closestvalueinlist Golf!
by Masem (Monsignor) on May 29, 2001 at 18:41 UTC

Here's a 62 char solution, imploying half a schwatzian transform:
sub g{
#234567890123456789012345678901234567890123456789012345678901234567890
my$c=pop;(sort{@$a[1]<=>@$b[1]}map{[$_,abs($_$c)]}@_)[0]>[0]
}
Dr. Michael K. Neylon  mneylonpm@masemware.com

"You've left the lens cap of your mind on again, Pinky"  The Brain
 [reply] [d/l] 
Re: Closestvalueinlist Golf!
by mr_mischief (Monsignor) on May 31, 2001 at 06:55 UTC

Not the shortest, but this was what was obvious to me...
sub c {
#_123456789_123456789_123456789_123456789_12345
$i=pop;for(@_){$c=$_ if abs$i$c>abs$i$_}$c
}
Chris  [reply] [d/l] 
Re: Closestvalueinlist Golf!
by ChOas (Curate) on May 29, 2001 at 18:13 UTC

Hey!,
I think it`s shorter, and I got no spaces...
But I don`t know how to count...
sub g{%_=map{abs($_[1]$_[$_]),$_}(0..@_2);$_[$_{(sort{$a<=>$b}keys%
+_)[0]}]}
GreetZ!,
print "profeth still\n" if /birddevil/;  [reply] [d/l] 

 [reply] 
Re: Closestvalueinlist Golf!
by ChOas (Curate) on May 29, 2001 at 18:46 UTC

55 ... and now I give up....
sub g{%_=map{abs$_[1]$_,$_}@_;$_{(sort{$a<=>$b}keys%_)[1]}}
GreetZ!,
print "profeth still\n" if /birddevil/;  [reply] [d/l] 
Re: Closestvalueinlist Golf!
by mr.nick (Chaplain) on May 30, 2001 at 22:47 UTC

Hm. I used a different approach, and if my eyes don't deceive me, it's the lowest score yet. Update: No it's not ... damn:
sub g {
# 1 2 3 4 5
# 1234567890123456789012345678901234567890123456789012
$n=pop;$,=pop;$,=abs$n$_<abs$n$,?$_:$,for@_;$,
}
for a score of 48.
 [reply] [d/l] 