develooper Front page | perl.perl5.porters | Postings from February 2019

[perl #133781] Clarify DOC for split. Section about splitting emptystring

From:
Tony Cook via RT
Date:
February 13, 2019 22:47
Subject:
[perl #133781] Clarify DOC for split. Section about splitting emptystring
Message ID:
rt-4.0.24-9003-1550098053-150.133781-15-0@perl.org
On Sun, 20 Jan 2019 13:19:46 -0800, davidnicol@gmail.com wrote:
> >
> >
> > > print map{ defined? "YES" :"NO"} split('b', "b"); # NOTHING IS PRINTED
> >
> 
> It initially seems like this should have printed a YES for an empty field
> at the beginning.
> 
> The documentation for split doesn't mention that a string matching the
> delimiter will yield an empty list, aside from that this behavior is
> implied by the bit about trailing empty fields being dropped -- which seems
> buried in the discussion of the limit argument, and therefore not clearly
> controlling in the fact of the more clearly stated "An empty leading field
> is produced when there is a positive-width match at the beginning of EXPR."

The thing is this behaviour is controlled by LIMIT:

 $ perl -MData::Dumper -e 'print Dumper([ split /b/, "b" ])'
 $VAR1 = [];
 $ perl -MData::Dumper -e 'print Dumper([ split /b/, "b", 2 ])'
 $VAR1 = [
          '',
          ''
        ];
 $ perl -MData::Dumper -e 'print Dumper([ split /b/, "b", -1 ])'
 $VAR1 = [
          '',
          ''
        ];

so I think it needs to stay with the LIMIT discussion.

The documentation is fairly explicit that all fields might be dropped with a zero limit:

... if all fields are empty, then all fields are considered to
be trailing (and are thus stripped in this case)

> I suggest throwing yet another clause in there, to give
> 
> An empty leading field (which will probably get dropped when it is all
> there is, see below) is produced when there is a positive-width match at
> the beginning of EXPR.

I think this case is well covered by the above.

I'll close this ticket next week(ish) unless there's an objection.

Tony


---
via perlbug:  queue: perl5 status: open
https://rt.perl.org/Ticket/Display.html?id=133781



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About