in reply to alternate array elements
$ perl -E 'say grep {++$i %2} <a b c d e f g>' aceg $ perl -E 'say grep {$i++ %2} <a b c d e f g>' bdf
They only nasty thing about this solution is that it requires a variable (here $i) in the outer scope
A working Perl 6 solution:
$ perl6 -e 'my @a = "a".."g"; say @a[0, *+2 ... +@a]' aceg
An idiomatic Perl 6 solution (but not yet implemented in Rakudo):
@a[0, *+2 ... *]
Another working Perl 6 solution, which is a variation of the grep theme but without the need of an external variable:
say <a b c d e f g>.pairs.grep({.key !% 2})>>.value
(Updated: Added Perl 6 examples)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: alternate array elements
by mrdvt92 (Acolyte) on May 12, 2023 at 17:39 UTC | |
by LanX (Saint) on May 12, 2023 at 21:19 UTC |