leszekdubiel has asked for the wisdom of the Perl Monks concerning the following question:

In Javascript I go for array like this:

let myArray = ["alfa", "beta", "gamma"]; myArray.forEach( (elem, position) => console.log(`at position ${position} th +ere is ${elem}`) )

and I get the result:

at position 0 there is alfa at position 1 there is beta at position 2 there is gamma

The same in perl:

my @myArray = qw(alfa beta gamma) grep { warn "at position ??? there is $_\n" } @myArray;

result:

at position ??? there is alfa at position ??? there is beta at position ??? there is gamma

What to put instead of question marks?

Replies are listed 'Best First'.
Re: grep { } positional number of element
by Your Mother (Archbishop) on Oct 23, 2022 at 21:23 UTC

    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;

      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.

Re: grep { } positional number of element
by sectokia (Friar) on Oct 23, 2022 at 21:35 UTC

    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
      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)?)

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

      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.

      I did not know that, thanks.

Re: grep { } positional number of element
by jo37 (Curate) on Oct 23, 2022 at 21:52 UTC

    Or, if you dislike loops:

    use v5.16; use List::MoreUtils 'reduce_u'; my @array = qw(alpha beta gamma); reduce_u {say "at position $_ there is $b"} @array;

    Greetings,
    -jo

    $gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$
Re: grep { } positional number of element
by LanX (Saint) on Oct 23, 2022 at 22:14 UTC
    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

Re: grep { } positional number of element
by tybalt89 (Monsignor) on Oct 23, 2022 at 22:47 UTC
    #!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11147614 use warnings; use List::AllUtils qw( pairwise ); my @myArray = qw(alfa beta gamma); pairwise { warn "at position $a there is $b\n" } @{[ 0 .. $#myArray ]}, @myArray;
Re: grep { } positional number of element
by tybalt89 (Monsignor) on Oct 23, 2022 at 23:45 UTC

    In plain old ordinary basic perl (whatever that is) I'd just do:

    #!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11147614 use warnings; my @myArray = qw(alfa beta gamma); warn "at position $_ there is $myArray[$_]\n" for 0 .. $#myArray;