Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Switch the odd/even elements of an array

by wind (Priest)
on Mar 04, 2011 at 20:31 UTC ( #891512=note: print w/replies, xml ) Need Help??


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

use xor on the array indexes:
my @foo = (1..8); my @bar = @foo[map {$_ ^ 1} (0..$#foo)]; print "@bar";
Outputs
2 1 4 3 6 5 8 7
- Miller

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://891512]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2017-07-26 04:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I came, I saw, I ...
























    Results (383 votes). Check out past polls.