On a side note, your code

$sum += $_ for split ' ', "5 6 7 8\n", $K;

is broken! *

Because split will always also return the unsplitted remaining rest which of course can't be added.

DB<20> use Data::Dump qw/dd/; DB<21> $str = join " ", 10..15 DB<22> dd ["$_",( split ' ', $str, $_)] for 0..6 [0, 10 .. 15] [1, "10 11 12 13 14 15"] [2, 10, "11 12 13 14 15"] [3, 10, 11, "12 13 14 15"] [4, 10, 11, 12, "13 14 15"] [5, 10 .. 13, "14 15"] [6, 10 .. 15]

and your desired count behavior for LIMIT=0 is actually mapped on LIMIT=1

If LIMIT is specified and positive, it represents the maximum number of fields into which the EXPR may be split; in other words, LIMIT is one greater than the maximum number of times EXPR may be split. Thus, the LIMIT value 1 means that EXPR may be split a maximum of zero times, producing a maximum of one field (namely, the entire value of EXPR).

In other words LIMIT may have a complicated design, but it's consistent.

see my other post Re: discussion: What should split( /PATTERN/, EXPR, 0 ) return better? (split-LIMIT = count and/or flag) for a working example.

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!

*) tybalt89's hint was maybe a bit too subtle ;)


In reply to Re: discussion: What should split( /PATTERN/, EXPR, 0 ) return better? (LIMIT is consistent! ) by LanX
in thread discussion: What should split( /PATTERN/, EXPR, 0 ) return better? by rsFalse

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.