in reply to grep { } positional number of element

As others already explained you showed the equivalent of a foreach in JS but asked for a grep, where at best a map would make sense.

And you already got excellent replies!

But for completeness Perl is flexible enough to create your own syntactic sugar for this task

use v5.12; use warnings; use Data::Dump qw/pp dd/; our $c; # count sub cmap(&@) { my $block = shift @_ ; local $c=-1; map { $c++; &$block } @_; } # -------- DEMO my @myArray = qw(alfa beta gamma); say "--- used as loop"; cmap { say "at position $c there is $_" } @myArray; say "--- used as map"; dd cmap { [ $c => $_] } @myArray;

--->

--- used as loop at position 0 there is alfa at position 1 there is beta at position 2 there is gamma --- used as map ([0, "alfa"], [1, "beta"], [2, "gamma"])

Cheers Rolf
(addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
Wikisyntax for the Monastery