Help for this page

Select Code to Download


  1. or download this
    sub nn_single {
        @_=sort{$a<=>$b}@_;
        $_{abs$_[$_]-$_[$_-1]}=[$_-1,$_]for 1..$#_;
        @_[@{$_{(sort{$a<=>$b}keys%_)[0]}}]
    }
    
  2. or download this
    sub nn_dup {
        my$s=sub{sort{$a<=>$b}keys%_};
        @_{@_}=0;
    ...
        $_{abs$_[$_]-$_[$_-1]}=[$_-1,$_]for 1..$#_;
        @_[@{$_{(&$s)[0]}}]
    }