in reply to Switch the odd/even elements of an array
my @bar = @foo [map {$_ + ($_ % 2) * -2 + 1} 0 .. $#foo]; # Destroys the array. Doesn't work in situ. my @bar; push @bar, reverse splice @foo, 0, 2 while @foo; # Doesn't work in situ. use List::MoreUtils ':all'; my (@bar, @t); my $i = natatime, 2, @foo; push @bar, reverse @t while @t = $i->(); # Doesn't work if there's an undefined value on an even position. my @bar = do {my $t; map {defined $t ? do {my $x = $t; undef $t; ($_, +$x)} : do {$t = $_; ()}} @foo};
|
|---|