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;
| [reply] [d/l] [select] |
|
|
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.
| [reply] [d/l] |
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
| [reply] [d/l] [select] |
|
|
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)?) | [reply] [d/l] |
|
|
| [reply] [d/l] |
|
|
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!
| [reply] [d/l] [select] |
|
|
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.
| [reply] [d/l] [select] |
|
|
|
|
|
|
| [reply] |
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$
| [reply] [d/l] |
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"])
| [reply] [d/l] [select] |
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;
| [reply] [d/l] |
Re: grep { } positional number of element
by tybalt89 (Monsignor) on Oct 23, 2022 at 23:45 UTC
|
#!/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;
| [reply] [d/l] |