in reply to How to get the index of smallest whole number in an array?

#!/usr/bin/perl -l # https://perlmonks.org/?node_id=1217669 use strict; use warnings; my @array = ('3','4','71','1', '-598', '-100203'); my ($answer) = map $_->[0], sort { $a->[1] <=> $b->[1] } grep $_->[1] >= 0, map [ $_, $array[$_] ], 0 .. $#array; print $answer;

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

    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:  <%-{-{-{-<

Re^2: How to get the index of smallest whole number in an array?
by johngg (Canon) on Jul 01, 2018 at 11:06 UTC

    The extra maps could be dispensed with I think.

    johngg@abouriou ~/perl/Monks $ perl -Mstrict -Mwarnings -E ' my @arr = ( 3, 4, 71, 1, -598, -100203 ); say for ( sort { $arr[ $a ] <=> $arr[ $b ] } grep { $arr[ $_ ] >= 0 } 0 .. $#arr )[ 0 ];' 3

    Cheers,

    JohnGG

      I love reading through answers to even "simple" questions because I get to see new ways to solve problems and new syntax. This code is a prime example. Embedding code in a for loop like this is something I've never even thought of, but it makes sense that you can do that.

      But, what's with the [ 0 ] at the end?

      my @arr = ( 3, 4, 71, 1, -598, -100203 ); say for ( sort { $arr[ $a ] <=> $arr[ $b ] } grep { $arr[ $_ ] >= 0 } 0 .. $#arr )[ 0 ]; # okay. what's with the [ 0 ] ??????

      I really want to know!

      
      
      
      $state{tired}?sleep(40):eat($food);
        ... what's with the [ 0 ] at the end?

        If you liked that, take another look at kcott's  ( ... )[0][0] approach. That said, it's still not an O(n) solution and therefore not, in general, and all things being equal, to be preferred IMHO.


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

        Update:Corrected ordering. Thanks to AnomalousMonk

        The for is unnecessary. This is equivalent:say ( ... )[0].

        The contents of the parens creates an order list smallest to largest; the [0] selects the first element of that list.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
        In the absence of evidence, opinion is indistinguishable from prejudice. Suck that fhit