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.
| [reply] [d/l] [select] |
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
| [reply] [d/l] |
Re: discussion: What should split( /PATTERN/, EXPR, 0 ) return better? (LIMIT is consistent! )
by LanX (Saint) on Oct 12, 2017 at 22:41 UTC
|
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.
*) tybalt89's hint was maybe a bit too subtle ;)
| [reply] [d/l] [select] |
|
|
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.
| [reply] |
|
|
| [reply] [d/l] |
|
|
|
|
|
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". ;-)
| [reply] |
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?
| [reply] [d/l] |
|
|
Do I want anything or something depends on a variable (input).
| [reply] |
|
|
my @results;
if ($condition){
@results = split ' ', $pattern, $max;
}
else {
# do something different, or don't
}
| [reply] [d/l] |
|
|
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
| [reply] |
Re: discussion: What should split( /PATTERN/, EXPR, 0 ) return better?
by ikegami (Patriarch) on Oct 13, 2017 at 16:34 UTC
|
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]);
}
| [reply] [d/l] [select] |
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
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. | [reply] [d/l] [select] |
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. | [reply] |