in reply to alternate array elements

Another way to do it:

$ perl -le' use List::MoreUtils "natatime"; my @x = "a" .. "z"; my $iter = natatime 2, @x; my @even; while ( my @y = $iter->() ) { push @even, $y[ 0 ]; } print "@even"; ' a c e g i k m o q s u w y

Replies are listed 'Best First'.
Re^2: alternate array elements
by Anonymous Monk on May 20, 2010 at 15:14 UTC
    Thanks all!

    toolic I didn't know of supersearch so that's useful

    cdarke I like the keys approach, hadn't thought of that at all and presumably it's fast, will try that next time if I don't need to preserve order.

    For posterity, I decided I actually needed the ODD indices (fwiw, the array has duplicate values in it and I'm porting VBA code which uses 1-based arrays, hence want to keep $all[0] as it = undef) so I used:

    @odd = @all[grep($_%2==0, 0..$#all)];

    or seomething like that. I guess the not would be faster tho, but I think it'd confuse my readers more, they'll already be struggling with grep, % and $#!!

      gah should have said evens - basically edited solution by almac at the top now