http://www.perlmonks.org?node_id=83972


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

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 strict-compliant 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$a-pop}
And we are at 36 chars.

Jeroen
"We are not alone"(FZ)

Replies are listed 'Best First'.
Re: Re: Closest-value-in-list Golf!
by Anonymous Monk on May 30, 2001 at 00:08 UTC
    Why do you not count "use PDL"?
      Should have mentioned that. Add 7 chars.

      Use of modules actually should disqualify a golf, methinks. It's just that I've come to like matrix calc, and couldn't resist such a solution.