develooper Front page | perl.perl5.porters | Postings from April 2007

perlfunc.pod/split; concerning trailing fields

Thread Next
From:
webmasters
Date:
April 22, 2007 03:43
Subject:
perlfunc.pod/split; concerning trailing fields
Message ID:
74666683-8356-4CDA-BBB2-06E169995DD8@ctosonline.org
This paragraph in perlfunc/split is misleading (and simply wrong in  
places):

<perlfunc>
Empty leading (or trailing) fields are produced when there are positive
width matches at the beginning (or end) of the string; a zero-width  
match
at the beginning (or end) of the string does not produce an empty field.
For example:

    print join(':', split(/(?=\w)/, 'hi there!'));

produces the output 'h:i :t:h:e:r:e!'.
</perlfunc>

A zero-width match at the end of a string *does* produce a null  
field. And since empty trailing fields are only produced when limit  
is big enough or negative, I think we ought to put a limit of -1 in  
the examples to illustrate more clearly exactly what fields are  
produced.

Here's my suggestion:

--- /usr/local/lib/perl5/5.8.8/pods/perlfunc.pod	2007-01-09  
20:24:24.000000000 -0800
+++ perlfunc.pod	2007-04-21 14:33:56.000000000 -0700
@@ -5216,14 +5216,22 @@

  produces the output 'h:i: :t:h:e:r:e'.

-Empty leading (or trailing) fields are produced when there are positive
-width matches at the beginning (or end) of the string; a zero-width  
match
-at the beginning (or end) of the string does not produce an empty  
field.
-For example:
+Empty leading fields are produced when there are positive-width  
matches at
+the beginning of the string; a zero-width match at the beginning of
+the string does not produce an empty field. For example:

     print join(':', split(/(?=\w)/, 'hi there!'));

-produces the output 'h:i :t:h:e:r:e!'.
+produces the output 'h:i :t:h:e:r:e!'. However, empty I<trailing>  
fields
+are produced when there is a match at the end of the string (and when
+LIMIT is given and is not 0), I<regardless of the length of the match.>
+For example:
+
+   print join(':', split(//,   'hi there!', -1));
+   print join(':', split(/\W/, 'hi there!', -1));
+
+produce the output 'h:i: :t:h:e:r:e:!:' and 'hi:there:', respectively,
+both with an empty trailing field.

  The LIMIT parameter can be used to split a line partially



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