in reply to Re: split an array in two
in thread split an array in two

sub split_array_in_two{ [@_[0..@_/2-1]], [@_[@_/2..@_]] }

Replies are listed 'Best First'.
Re^3: split an array in two
by Roy Johnson (Monsignor) on Apr 28, 2005 at 14:50 UTC
    You're reaching beyond the end of the array, resulting in an extra undef getting stuck on the end of your second arrayref. Also, a one element list gets replicated in both resulting arrays.

    Caution: Contents may have been coded under pressure.
      #must be: [@_[0..@_/2-1]], [@_[@_/2..@_-1]]


        Here's the test code:
        for (0..5) { print map("<@$_>", split_array_in_two(1..$_)), "\n"; }
        Here's the output:
        <><> <1><1> <1><2> <1><2 3> <1 2><3 4> <1 2><3 4 5>
        One-element input is still duplicated. Don't use @_ as an index.

        Caution: Contents may have been coded under pressure.