in reply to Re^3: How to get the index of smallest whole number in an array?
in thread How to get the index of smallest whole number in an array?

I have voted only for your solution. Because while everyone is showing solutions with sort, map grep. I just don't understand that after 30 posts finally someone is talking some sense.

If I would have needed to solve something like this, I would have come up with a simple loop. Job done, move on:

use strict ; use warnings ; my @arr = ( 3, 4, 71, 1, 1.5, -598, -100203, 0.5, -2, -1.5 ); my $result ; while (my ($ix, $val) = each @arr) { next if ( $val < 0 || int $val != $val || ($result && $val > $resu +lt->[0]) ) ; $result = [ $val, $ix ] ; } if ( $result ) { print $result->[0] . " at " . $result->[1] . "\n" ; } __END__ 1 at 3

Replies are listed 'Best First'.
Re^5: How to get the index of smallest whole number in an array?
by AnomalousMonk (Archbishop) on Jul 03, 2018 at 21:38 UTC

    Congratulations: Another O(n) solution! Please note that the each ARRAY syntax is only available from Perl version 5.12 onward.


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