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

G'day sohamsg90,

Welcome to the Monastery.

I'm not really trying to nitpick; however, simply saying you wanted the index of the smallest "non-negative integer", would have precluded the need to provide a definition of what you meant by "whole numbers" — which, incidentally, differs from what is generally understood by that term (e.g. -5 is a whole number; -0.5 isn't) — and made the whole thing less confusing. I had to read it twice to make sure I understood what you were getting at.

You've added two negative integers to your test data: that's good. However, you don't have any non-integer values to test: that's less good.

The following one-liner includes fractional test values and code to handle them.

$ perl -E 'my @x = qw{3 4 71 1 -598 -100293 0.5 -0.5}; say +(sort { $a +->[1] <=> $b->[1] } map $x[$_] >= 0 && $x[$_] == int $x[$_] ? [ $_ => + $x[$_] ] : (), 0 .. $#x)[0][0]' 3

Well, that was my actual test. Here it is again, in a somewhat more readable format.

$ perl -E ' my @x = qw{3 4 71 1 -598 -100293 0.5 -0.5}; say +( sort { $a->[1] <=> $b->[1] } map $x[$_] >= 0 && $x[$_] == int $x[$_] ? [ $_ => $x[$_] ] : (), 0 .. $#x )[0][0] ' 3

I also tested this with the first element of @x changed from 3 to 0.3, and also changed to -0.3. The result was the same for all three runs.

You'll note that the general approach is similar to some other solutions already posted.

— Ken

Replies are listed 'Best First'.
Re^2: How to get the index of smallest whole number in an array?
by Laurent_R (Canon) on Jul 01, 2018 at 08:11 UTC
    Not nitpicking either, but consider that the OP may not be an English native speaker. In a number of languages, integer is translated by something meaning "whole number." For example: número entero in Spanish and nombre entier in French. Granted, Perl Monks is an English-language forum, but it is attended by many people whose mother-tongue is not English.

      Blame mathematicians and their predilection for inventing new nomenclature and usurping natural language to define them.

      From Wikipedia:

      Some definitions, including the standard ISO 80000-2,1 begin the natural numbers with 0, corresponding to the non-negative integers 0, 1, 2, 3, …, whereas others start with 1, corresponding to the positive integers 1, 2, 3, ….2345

      Texts that exclude zero from the natural numbers sometimes refer to the natural numbers together with zero as the whole numbers, but in other writings, that term is used instead for the integers (including negative integers).6


      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
        Thank you, BrowserUk, for the Wikipedia reference. When I originally wrote my post, I was thinking about adding something saying that, to many people, "whole numbers" often means positive integers (with or without 0), but I did not because I wasn't entirely sure whether it was not a personal bias of mine. Your Wikipedia quote shows that it wasn't just my personal impression.

        Update:Fixed a typo in the last sentence.

      G'day Laurent,

      We've always got along well in the past, so I won't automatically assume this was intended as a personal attack. Unfortunately, what you've written comes across as arrogant: something I never find to be acceptable.

      You presumed to know what I hadn't considered; you presumed to tell me what to consider; and, to top it all off, you presumed to instruct on the makeup of the Monastery.

      I'm hoping your post was simply poorly worded.

      — Ken

        Hi dear Ken,

        my answer to you was certainly not meant as a personal attack, and I have absolutely no reason whatsoever to launch any personal attack against you. So I'm really sorry if you perceived it as an attack. And I certainly did not want to be arrogant either, I'm really sorry again if I appeared to be.

        I really did not presume to instruct you to do anything, nor to teach any lesson. My only factual point was that "whole numbers" may not mean exactly the same thing to everyone, especially to people whose mother tongue is not English. At least, the Spanish, French, Italian and Portuguese people (and probably a number of others) use the literal translation of "whole number" into their own language to render the English (or Latin) word "integer," including in highly technical computer science writings.

        I am really sorry if my post was poorly worded, I did not think it was, but, you know, English is not my mother tongue, so I may goof from time to time despite my best efforts.

        Wow. The only arrogance I see here is yours.

        Your "not-a-nit-pick" nit-pick: "e.g. -5 is a whole number;" is -- in many non-English natural language settings, and many strictly mathematical settings -- entirely WRONG! And all the dressing you wrapped around it, presumptive, unnecessary fluff; and your solution: stupidly inefficient and pointlessly complex.

        Laurent's oh-so-gentle reminder that your language, viewpoint and understanding, may differ from that of the OPs; was as polite as it is possible to be, whilst pointing out to a stranger, that they just might be being myopic, to the point of coming close to xenophobic.

        And that was just your arrogance in your original reply to the OP; your reply to Laurent is in another class entirely. The latter is quite simply the worst case of attempted misdirection-by-attacking-the-author-of-the-message-because-you-have-no-legitimate-answer-to-and-are-embarrassed-by-the-message as I've seen here in a long time.

        As for your exposition of what you "find acceptable", it is the very definition of that which you decry as unacceptable. It wouldn't be out of place in a tweet by the MOTUS.

        I'm hoping your latter reply was an uncharacteristic aberration and you'll re-read Laurent's post when in a less defensive mood and correct it accordingly.


        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
          A reply falls below the community's threshold of quality. You may see it by logging in.
Re^2: How to get the index of smallest whole number in an array?
by Anonymous Monk on Jul 01, 2018 at 15:04 UTC
    What does the empty list :() mean there?

      The () there is part of a ternary -

      map $x[$_] >= 0 && $x[$_] == int $x[$_] ? [ $_ => $x[$_] ] : (),

      - where the current element of the @x array is tested the see if it is greater or equal to zero and if it is a whole number. If true an anonymous array is passed to the sort routine, if false then an empty list (i.e. nothing) is passed. This construct operates pretty much like grep to filter out any values that don't satisfy the conditions.

      Cheers,

      JohnGG

      The explanation by ++JohnGG pretty much nails it.

      I might just add, by way of clarification of:

      "This construct operates pretty much like grep to filter out any values that don't satisfy the conditions."

      grep passes on its arguments unaltered if they satisfy the condition in BLOCK or EXPR:

      $ perl -E 'say for grep length, qw{x y z}' x y z

      map, on the other hand, passes on whatever the BLOCK or EXPR evaluates to for each argument:

      $ perl -E 'say for map length, qw{x y z}' 1 1 1

      You may already be aware of this; however, I thought it was worth pointing out: another reader may not be fully across this distinction.

      — Ken