It's possible to combine the grep expression with the preceding map expression:

c:\@Work\Perl\monks>perl -wMstrict -le "my @ra = qw(13 4 71 2 -598 -100203); ;; my ($i_lwn) = map $_->[0], sort { $a->[1] <=> $b->[1] } map { $ra[$_] >= 0 ? [ $_, $ra[$_] ] : () } 0 .. $#ra ; ;; if (defined $i_lwn) { print qq{\$ra[$i_lwn] == lwn $ra[$i_lwn]}; } else { print 'no lowest whole number in array'; } " $ra[3] == lwn 2

Update: This approach returns the lowest index of more than one LWN in an array (assuming sort is stable, which it is IIUC the documentation). I'm not sure what the simplest approach to changing this code to obtain the highest index of multiple LWNs would be. I've tested it only partially, but it may be as simple a putting a reverse call in front of the  0 .. $#ra expression at the bottom of the map stack.


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


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