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

#must be: [@_[0..@_/2-1]], [@_[@_/2..@_-1]]


Replies are listed 'Best First'.
Re^5: split an array in two
by Roy Johnson (Monsignor) on Apr 28, 2005 at 15:20 UTC
    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.
      I am not quite sure that @_ is so bad ...
      sub _split{ @_ == 1 and $#_++; [@_[0..@_/2-1]], [@_[@_/2..@_-1]] } __STDOUT__ <><> <1><> <1><2> <1><2 3> <1 2><3 4> <1 2><3 4 5>


        Apart from the fact that you're still not getting the OP's results, and you now (merely) generate a warning for the 1-element case, you're going through some rather silly gyrations to use @_ as an index instead of $#_. Using @_ as an index is still a bad idea, like using a screwdriver as a hammer. You might eventually get the result you want, but it's needlessly difficult.

        Caution: Contents may have been coded under pressure.