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

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.

Replies are listed 'Best First'.
Re^4: split an array in two
by sh1tn (Priest) on Apr 28, 2005 at 15:14 UTC
    #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.
        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>