in reply to grep { } positional number of element

grep only goes through the actual elements of whatever is passed to it. No index is passed. You might want something like–

my @myArray = qw(alfa beta gamma); grep { print "$myArray[$_] at $_\n" } 0 .. $#myArray;

But that is *atrocious* style. grep is for filtering, not iterating. Use a for loop instead. Terse postfix version, you should generally prefer the expanded with a block–

print "$myArray[$_] at $_\n" for 0 .. $#myArray;

Replies are listed 'Best First'.
Re^2: grep { } positional number of element
by Fletch (Bishop) on Oct 24, 2022 at 02:12 UTC

    Seconding the style condemnation; similarly using map would be just as incorrect.

    Use grep to filter, map to transform, and for to iterate (or one of the List::* modules for anything else more specialized like a reduce).

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.