Every other

Damian Conway damian at conway.org
Fri Oct 30 21:07:08 GMT 2009


Mark Fowler <mark at twoshortplanks.com> wanted:

> I have an array in Perl 5 [2].  I want *every* *other* element from it.

Not especially fast, but very easy:

    use List::Maker;

    my @new = @old[ <0,2,..$#old> ];


> There's very nice syntax for this in Perl 6, isn't there?

Yes. Several in fact. The nicest is:

    my @new = @old[0,2...*];

That doesn't work yet in the current alpha release of Rakudo, but this
form does:

    my @new = @old[ 0...*+2, 6 ];


Damian



More information about the london.pm mailing list