in reply to Switch the odd/even elements of an array
# a recursive approach, similar to BrowserUK's sub swap1 { my @t = ( pop, pop ); ( @_ ? &swap1 : (), @t ) } # an iterative approach sub swap2 { my @t; unshift @t, ( pop, pop ) while @_; @t }
I've tried to avoid any reference to the index whatsoever, and also reverse, which I think is too "obvious".
BrowserUK's solution is superior in that it avoids temporary variables as well.
|
|---|