in reply to Switch the odd/even elements of an array

Another solution. The input array is known to have an even number of elements. Just take pairs off of the top of @input, swap 'em, push 'em to a temp array, then return that array when done. No need for recursion, indexing or anything "tricky".
#!/usr/bin/perl -w use strict; my @input = (1,2,3,4,5,6,7,8); print swap_pairs(@input); # prints: 21436587 sub swap_pairs { my @swapped_pairs; while ( my ($x,$y) = splice(@_,0,2) ) { push @swapped_pairs,($y,$x); } return @swapped_pairs; }