in reply to Switch the odd/even elements of an array
my %numbers = 1..8; my @sorted = map { $numbers{$_} => $_ } sort keys %numbers; print join ", ", @sorted;
Prints 2, 1, 4, 3, 6, 5, 8, 7
We rely on Perl's implicit conversion of lists to hashes, and use sort to get them back out of the hash in the desired order.
|
|---|