in reply to grep { } positional number of element

grep is wrong thing to use here, you could use each:

my @array = ( "Alfa","beta","gamma"); while (my ($index, $element) = each(@array)) { print "$index => $element\n"; }

Output:

0 => Alfa 1 => beta 2 => gamma

Replies are listed 'Best First'.
Re^2: grep { } positional number of element
by ibm1620 (Hermit) on Oct 23, 2022 at 22:27 UTC
    Or, if you are running the latest version of Perl, v5.36, you could go experimental and use the new for list and indexed function>
    use v5.36; use builtin qw/indexed/; no warnings q/experimental::builtin/; no warnings q/experimental::for_list/; my @array = ( "Alfa","beta","gamma"); for my ($index, $element) (indexed @array) { print "$index => $element\n"; }
    (Incidentally, is there any difference between indexed(@array) and each(@array)?)
Re^2: grep { } positional number of element
by Your Mother (Archbishop) on Oct 23, 2022 at 23:45 UTC

    ++ 12 years of using 5.8 broke my brain. I didn’t even remember you could use each on arrays now.

Re^2: grep { } positional number of element
by Bod (Parson) on Oct 23, 2022 at 22:29 UTC
    you could use each

    Thank you for the reminder...
    Just the other day I had a situation where each (called on a hash) would have been a good solution. But I completely forgot about its existence having not looked at it for many, many years. I don't think I have ever used each but certainly need to ensure I don't forget it again!

      I think there is, or, at least, *was*, good reason why each was erased from our concsiousness. I do not know what the state of these bugs is today. I was definetely bitten by its non-reentrancy /sic/ several times in nested each loops.

        good reason why each was erased from our concsiousness [sic]

        Thanks for bringing that to our attention 🙂

        It seems that each is OK provided we know for sure that the array or hash that is being iterated is not being modified during the iteration. Much of the time we can be sure of that I think. But, of course, it is easy to overlook that the data could be modified, particularly if each is used in a module.

Re^2: grep { } positional number of element
by bliako (Abbot) on Oct 24, 2022 at 13:31 UTC

    I did not know that, thanks.