in reply to Re: 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?

What does the empty list :() mean there?
  • Comment on Re^2: How to get the index of smallest whole number in an array?

Replies are listed 'Best First'.
Re^3: How to get the index of smallest whole number in an array?
by johngg (Canon) on Jul 01, 2018 at 15:33 UTC

    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

Re^3: How to get the index of smallest whole number in an array?
by kcott (Archbishop) on Jul 02, 2018 at 11:39 UTC

    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