rsFalse has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

Today I have used split( /PATTERN/, EXPR, 0 ) (LIMIT = 0), and it returned an answer equivalent to what does split( /PATTERN/, EXPR ) return. Such behaviour is documented split: "If LIMIT is omitted (or, equivalently, zero), then...".

I was solving a task, and I've forgotten this LIMIT=0 behaviour, and I was expecting to get an empty list. In my opinion this behaviour would be good also. That has some logic. One can interpret "LIMIT" as a non-negative number representing a number of chunks of string one wants to get. Then if one wants to get as much as possible chunks, he do not use LIMIT (unlimited chunks), or use verbose keywors like LIMIT="MAX". What are your opinion?

The task was: to arithmetically add K (K>=0) first numbers which are written in a string form separated by whitespaces, e.g. "5 6 7 8\n". For this task I wrote:
my $K = <>; my $sum = 0; $sum += $_ for split ' ', "5 6 7 8\n", $K; print $sum;
and got correct answers for all K > 0, but not K == 0.
K sum 0 26 1 5 2 11 3 18 4 26 5 26

Replies are listed 'Best First'.
Re: discussion: What should split( /PATTERN/, EXPR, 0 ) return better?
by Laurent_R (Canon) on Oct 12, 2017 at 21:46 UTC
    I was solving a task, and I've forgotten this LIMIT=0 behaviour, and I was expecting to get an empty list. In my opinion this behaviour would be good also. That has some logic.
    Yes, indeed, this has arguably some logic. It could admittedly have been done this way in the first place. But you could also argue that, in this case, the split function should return the whole string untouched. Is this behavior better or less desirable? I just don't know, and we could probably argue forever on that, couldn't we?

    OTOH, why would you use split (and probably fire the regex engine) if you don't want to split your string? It'd be quite useless and pretty inefficient.

    Then, of course, as afoken said, the current behavior will not change for backward compatibility reasons. So, my opinion (and yours) is quite irrelevant at this point in time.

Re: discussion: What should split( /PATTERN/, EXPR, 0 ) return better?
by tybalt89 (Monsignor) on Oct 12, 2017 at 19:52 UTC
    #!/usr/bin/perl -l # http://perlmonks.org/?node_id=1201251 use strict; use warnings; print "K sum"; for my $K ( 0..5 ) { my $sum = 0; $sum += ($_ // 0) for (split ' ', "5 6 7 8\n")[0 .. $K-1]; print "$K $sum"; }

    Try yours with strict and warnings and see what you get

Re: discussion: What should split( /PATTERN/, EXPR, 0 ) return better? (LIMIT is consistent! )
by LanX (Saint) on Oct 12, 2017 at 22:41 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!

Re: discussion: What should split( /PATTERN/, EXPR, 0 ) return better?
by afoken (Chancellor) on Oct 12, 2017 at 20:37 UTC

    As backwards compatibility is a huge goal in Perl, you can spawn endless discussions, but split's behaviour won't change in a backwards-incompatible way.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: discussion: What should split( /PATTERN/, EXPR, 0 ) return better?
by stevieb (Canon) on Oct 12, 2017 at 18:34 UTC

    It's much more efficient if you just don't split() if you don't want anything from its result. Why make the call at all if you want nothing returned?

      Do I want anything or something depends on a variable (input).
        my @results; if ($condition){ @results = split ' ', $pattern, $max; } else { # do something different, or don't }

        Do I want anything or something depends on a variable (input).

        Don't you mean dynamic variable?

        See I can say silly stuff too.

        What are your opinion?

        m//atch if you want to match, split if you want to split, or mysplit if neither satisfies thats programming

Re: discussion: What should split( /PATTERN/, EXPR, 0 ) return better?
by ikegami (Patriarch) on Oct 13, 2017 at 16:34 UTC

    People are always forgetting they can write subs.

    sub split0($;$$) { my $pattern = shift; my $s = @_ ? shift : $_; my $limit = @_ ? shift : -2; return $s if $limit == 0; return split($pattern, $s, $limit == -2 ? 0 : $limit); }
    or
    sub split0($;$$) { @_ == 1 && return split($_[0]); @_ == 2 && return split($_[0], $_[1]); $_[2] == 0 && return $_[1]; return split($_[0], $_[1], $_[2]); }
Re: discussion: What should split( /PATTERN/, EXPR, 0 ) return better? (split-LIMIT = count and/or flag)
by LanX (Saint) on Oct 12, 2017 at 21:43 UTC

    UPDATE
    please see my other post here: Re: discussion: What should split( /PATTERN/, EXPR, 0 ) return better? (LIMIT is consistent! ) which makes most of the following reasoning obsolete (the code still works)


    Again I was struggling to understand what your question is (or rather your meditation).

    Yes I agree that it's not very orthogonal, if LIMIT > 0 is a count and LIMIT == 0 rather means undef like in "No Limit" .*

    I don't see where this concept stems from, probably some sed or awk semantic, (probably because undef is missing there ?)²

    But I agree with afoken that it's too late to change it.

    Anyway I would never have tried to solve it that way, rather

    DB<1> $str = join " ", 1..5 DB<2> for $k (0..6) { my $sum=0; $sum+= $_ for (split ' ', $str)[0.. +$k-1] ; print "$sum\n";} 0 1 3 6 10 15 15

    or rather

    DB<6> @split = (split ' ', $str) DB<7> for $k (0..6) { my $sum=0; $sum+= $_ for @split[0..$k-1]; prin +t "$sum\n";} 0 1 3 6 10 15 15

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

    update

    *) and you shouldn't forget that LIMIT =-1 has even one more non-count meaning

    ²) couldn't find similarities in awk or sed, but the reason is most probably that 0 and undef are both false in Perl, and the designers wanted to keep the symmetry.

Re: discussion: What should split( /PATTERN/, EXPR, 0 ) return better?
by rsFalse (Chaplain) on Oct 13, 2017 at 11:03 UTC
    Thank you all for your minds and discussion.

    I think I can add, that 'split' is a polymorphic function, and behaviour depends on a number of parameters and of values of these parameters. But it is counterintuitive for me, that function behaviour with 3 parameters can be more similar (in this case = identical) to 2-param function than to another behaviour with 3 parameters. Maybe only me have that broken intuition... Some cases are not very DWIMy (it depends on individual senses) so long learning curve with books and docs.