Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Switch the odd/even elements of an array

by wind (Priest)
on Mar 04, 2011 at 20:31 UTC ( [id://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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://891512]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-09-08 16:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.