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

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.

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

        The Python rule - "There should be one--
        and preferably only one --obvious way to do it."?
        No, I program in Perl.'difficult||easy' - meaningless words.
        Never-the-less - thanks for being polite!