$idx = $arr[0]; for(0..$#arr) { $idx = $_ if( ($arr[$idx]<0) || (($arr[$_] >= 0) && ($arr[$_] +< $arr[$idx])) ) }

This returns strange results for certain corner cases:

c:\@Work\Perl\monks>perl -wMstrict -le "use Data::Dump qw(pp); ;; for my $ar ( [ ], [ -1 ], [ -99 ], [ 0, -99 ], [ -99, 0 ], ) { my $idx = $ar->[0]; for (0..$#$ar) { $idx = $_ if( ($ar->[$idx]<0) || (($ar->[$_] >= 0) && ($ar->[$_] +< $ar->[$idx])) ); } print qq{(@$ar) -> lnn index of }, pp $idx; } " () -> lnn index of undef (-1) -> lnn index of 0 Use of uninitialized value in numeric lt (<) at -e line 1. (-99) -> lnn index of -99 (0 -99) -> lnn index of 0 Use of uninitialized value in numeric lt (<) at -e line 1. Use of uninitialized value in numeric lt (<) at -e line 1. Use of uninitialized value in numeric lt (<) at -e line 1. (-99 0) -> lnn index of -99
I'd hate to have to extend this to dealing with fractional numbers. The O(n) solutions here and here both return an unambiguous undef for lists containing no LNN, including empty lists.


Give a man a fish:  <%-{-{-{-<


In reply to Re^4: How to get the index of smallest whole number in an array? by AnomalousMonk
in thread How to get the index of smallest whole number in an array? by sohamsg90

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.