note
jmcnamara
Omitting leading whitespace this is 97 chars:
<code>sub nn_single {
@_=sort{$a<=>$b}@_;
$_{abs$_[$_]-$_[$_-1]}=[$_-1,$_]for 1..$#_;
@_[@{$_{(sort{$a<=>$b}keys%_)[0]}}]
}
</code>
I worked on this without looking at your solutions but it is very similar to your nn3. Great minds?
<br>
If the list contains duplicates then the following will work.
Omitting leading whitespace this is 114 chars.
<code>sub nn_dup {
my$s=sub{sort{$a<=>$b}keys%_};
@_{@_}=0;
@_=&$s;
%_=();
$_{abs$_[$_]-$_[$_-1]}=[$_-1,$_]for 1..$#_;
@_[@{$_{(&$s)[0]}}]
}
</code>
<b>Update:</b> I've updated this for a numerical sort and included [snowcrash]'s %_ hack. [danger], as usual, looks like the one to beat.
<br><br>
John.<br>
-- <br><br>
69570
69570