http://qs321.pair.com?node_id=665319


in reply to Golf/Perlish solution to this problem?

This solution is non-destructive and uses an array slice rather than push'ing, pop'ing and shift'ing.

use strict; use warnings; my @array = qw{ ace big cat dog egg fig gog hog }; my @newArray = @array[ reOrder( scalar @array ) ]; print qq{@newArray\n}; sub reOrder { my $length = shift; die qq{Array length not a multiple of 4\n} if $length % 4; my $low = 0; my $high = $length - 1; return map { $_ % 2 ? ( $high --, $low ++) : ( $low ++, $high --) } 1 .. $length / 2; }

The output.

hog ace big gog fig cat dog egg

Cheers,

JohnGG

Update: Re-factored subroutine to handle any length of array.

sub reOrder { my $length = shift; my $low = 0; my $high = $length - 1; my @slice = map { $_ % 2 ? ( $high --, $low ++) : ( $low ++, $high --) } 1 .. $length / 2; return @slice, $low == $high ? $low : (); }