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

Re: [perl #133781] Clarify DOC for split. Section about splittingempty string

Thread Previous | Thread Next
From:
David Nicol
Date:
January 20, 2019 21:19
Subject:
Re: [perl #133781] Clarify DOC for split. Section about splittingempty string
Message ID:
CAFwScO-_CHDk7pRSgDXrh11BNX8mSFeJcoh45CAj6EatF=0_3Q@mail.gmail.com
>
>
> > 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."


$ perl -le 'print scalar split /b/, "bx"'

2

$ perl -le 'print scalar split /b/, "b"'

0

$ perl -le 'print scalar split /b/, "b",-1'

2

$ perl -le 'print scalar split /b/, "b",1'

1

$ perl -le 'print scalar split /b/, "b",2'

2

$ perl -le 'print scalar split /b/, "b",3'

2


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.


Of course, that might just further tangle the spaghetti.

Thread Previous | Thread Next


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