What the heck... Yet Another non-O(n), sort-based solution, this one using a Guttman-Rosler transform (GRT).

c:\@Work\Perl\monks>perl -wMstrict -le "my @ra = qw(12 -3 4 71 5 -11 -0.99999 -598 -100203 0.99999 4); ;; my ($i_lnn) = map unpack('x[N] N', $_), sort map { $ra[$_] >= 0 && $ra[$_] == int($ra[$_]) ? pack('N N', $ra[$_] +, $_) : () } 0 .. $#ra ; ;; if (defined $i_lnn) { print qq{\$ra[$i_lnn] == lowest natural number $ra[$i_lnn]}; } else { print 'no lowest natural number in array'; } " $ra[2] == lowest natural number 4
Now with more fraction rejection. Still not to be preferred, in very general terms, to an O(n) solution IMHO. Still returns the lowest index of multiple LNNs (update: but a small change will return the highest). (I've started referring to these numbers as "lowest natural numbers" in deference to the N-pairs in the pack/unpack templates.) If you object to all the evaluations of  $ra[$_] in the first map statement, try
    map { my $e = $ra[$_];  $e >= 0 && $e == int($e) ? pack('N N', $e, $_) : () }
instead (tested), but I doubt there'll be much difference.

Update 1: Of course, this will only handle natural numbers in the range of a 32-bit N template specifier. 64-bit Perls have 64-bit Q specifiers. (Update: johngg points out that a big-end enforced Q> (note > modifier) is necessary for proper GRT sorting. I can't test this ATM.)

Update 2: Here's another, semi-tested variation to play around with:
    map $ra[$_] >= 0 && $ra[$_] == int($ra[$_]) && pack('N N', $ra[$_], $_) || (),


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


In reply to Re: 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.