in reply to discussion: What should split( /PATTERN/, EXPR, 0 ) return better?

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 ;)

  • Comment on Re: discussion: What should split( /PATTERN/, EXPR, 0 ) return better? (LIMIT is consistent! )
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: discussion: What should split( /PATTERN/, EXPR, 0 ) return better? (LIMIT is consistent! )
by rsFalse (Chaplain) on Oct 13, 2017 at 10:52 UTC
    So, if it returns unsplitted remaining, it is not broken, it is as I wanted, especially for doing arithmetic sum.

    I didn't understand about "your desired count behavior for LIMIT=0 is actually mapped on LIMIT=1". My desired behaviour don't have an equivalent value of any possible LIMIT.
      > , it is not broken, it is as I wanted

       $sum =  10 + 11 + "12 13 14 15" ;

      Orly?

      Did you see tybalt89's answer?

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

        So it's 33 and it's OK.