Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perldoc -f split lacks basic null example #7748

Closed
p5pRT opened this issue Jan 11, 2005 · 12 comments
Closed

perldoc -f split lacks basic null example #7748

p5pRT opened this issue Jan 11, 2005 · 12 comments

Comments

@p5pRT
Copy link

p5pRT commented Jan 11, 2005

Migrated from rt.perl.org#33766 (status was 'resolved')

Searchable as RT33766$

@p5pRT
Copy link
Author

p5pRT commented Jan 11, 2005

From @jidanni

Let's say we want to split on each character. We do perldoc -f split​:

  A pattern matching the null string (not to be confused with a null
  pattern "//", which is just one member of the set of patterns
  matching a null string) will split the value of EXPR into separate
  characters at each point it matches that way. For example​:

  print join('​:', split(/ */, 'hi there'));

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

  Using the empty pattern "//" specifically matches the null string,
  and is not be confused with the use of "//" to mean "the last
  successful pattern match".

By now the beginner is thoroughly frightened about the best way to
split on each character.

Apparently // and '' are both work. Anyway, the best choice should be
shown in an additional preceding example​:

  print join('​:', split('', 'hi there'));

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

Otherwise we jump into the deep end of the swimming pool straight to
complex examples.

@p5pRT
Copy link
Author

p5pRT commented Jan 12, 2005

From @ysth

On Tue, Jan 11, 2005 at 10​:22​:43PM -0000, Dan Jacobson wrote​:

Let's say we want to split on each character. We do perldoc -f split​:

A pattern matching the null string \(not to be confused with a null
pattern "//"\, which is just one member of the set of patterns
matching a null string\) will split the value of EXPR into separate
characters at each point it matches that way\.  For example​:

    print join\('​:'\, split\(/ \*/\, 'hi there'\)\);

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

Using the empty pattern "//" specifically matches the null string\,
and is not be confused with the use of "//" to mean "the last
successful pattern match"\.

By now the beginner is thoroughly frightened about the best way to
split on each character.

Apparently // and '' are both work. Anyway, the best choice should be
shown in an additional preceding example​:

    print join\('​:'\, split\(''\, 'hi there'\)\);

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

Otherwise we jump into the deep end of the swimming pool straight to
complex examples.

Perhaps you'd like to take a look at
http​://perlmonks.org/index.pl?node_id=322751
:)

@p5pRT
Copy link
Author

p5pRT commented Jan 12, 2005

The RT System itself - Status changed from 'new' to 'open'

@p5pRT
Copy link
Author

p5pRT commented Jan 14, 2005

From @jidanni

D> Perhaps you'd like to take a look at
D> http​://perlmonks.org/index.pl?node_id=322751
OK I will next time I dial up, but whatever is there ought to be
mentioned right there in perldoc -f split.

@p5pRT
Copy link
Author

p5pRT commented May 11, 2005

From @smpeters

[jidanni@​jidanni.org - Tue Jan 11 14​:22​:42 2005]​:
Apparently // and '' are both work. Anyway, the best choice should be
shown in an additional preceding example​:

I'd rather they decide the way that they prefer to split. Below is a
patch to demonstrate the null string splitting.

Inline Patch
--- pod/perlfunc.pod.old        Tue May  3 07:46:43 2005
+++ pod/perlfunc.pod    Wed May 11 12:09:22 2005
@@ -5079,7 +5079,13 @@

 Using the empty pattern C<//> specifically matches the null string, and is
 not be confused with the use of C<//> to mean "the last successful pattern
-match".
+match".  So, for C<split>, the following are equivolent:
+
+    print join(':', split(//, 'hi there'));
+
+    print join(':', split('', 'hi there'));
+
+and they produce 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

@p5pRT
Copy link
Author

p5pRT commented May 11, 2005

From ams@wiw.org

At 2005-05-11 17​:13​:29 -0000, perlbug-followup@​perl.org wrote​:

--- pod/perlfunc.pod.old Tue May 3 07​:46​:43 2005
+++ pod/perlfunc.pod Wed May 11 12​:09​:22 2005
@​@​ -5079,7 +5079,13 @​@​

Thanks, applied. (#24449)

-- ams

@p5pRT
Copy link
Author

p5pRT commented May 11, 2005

From @pjcj

On Wed, May 11, 2005 at 05​:13​:29PM -0000, Steve Peters via RT wrote​:

+match". So, for C<split>, the following are equivolent​:

s/equivolent/equivalent/

--
Paul Johnson - paul@​pjcj.net
http​://www.pjcj.net

@p5pRT
Copy link
Author

p5pRT commented May 11, 2005

From @tamias

On Wed, May 11, 2005 at 05​:13​:29PM -0000, Steve Peters via RT wrote​:

[jidanni@​jidanni.org - Tue Jan 11 14​:22​:42 2005]​:
Apparently // and '' are both work. Anyway, the best choice should be
shown in an additional preceding example​:

I'd rather they decide the way that they prefer to split. Below is a
patch to demonstrate the null string splitting.

--- pod/perlfunc.pod.old Tue May 3 07​:46​:43 2005
+++ pod/perlfunc.pod Wed May 11 12​:09​:22 2005
@​@​ -5079,7 +5079,13 @​@​

Using the empty pattern C<//> specifically matches the null string, and is
not be confused with the use of C<//> to mean "the last successful pattern
-match".
+match". So, for C<split>, the following are equivolent​:
+
+ print join('​:', split(//, 'hi there'));
+
+ print join('​:', split('', 'hi there'));
+
+and they produce the output 'h​:i​: :t​:h​:e​:r​:e'.

I don't understand the point of this patch.

The text implies that split(//) and split('') are equivalent because //
specifically matches the null string. That is not the case. split(//) and
split('') are equivalent because the first argument to split() is always
evaluated as a regular expression, except for the special case of ' '.

Of course split('') is equivalent to split(//), just as split('a|b') is
equivalent to split(/a|b/).

Ronald

@p5pRT
Copy link
Author

p5pRT commented May 11, 2005

From @iabyn

On Wed, May 11, 2005 at 02​:14​:28PM -0400, Ronald J Kimball wrote​:

Of course split('') is equivalent to split(//), just as split('a|b') is
equivalent to split(/a|b/).

And of course split(/a\b/) is *not* equivalent to split("a\b"), so it's
probably bad form to encourage people to think that regex literals and
string literals are always equivalent, and hence bad to show '' and // as
identical (even if they happen to be in this case).

--
A major Starfleet emergency breaks out near the Enterprise, but
fortunately some other ships in the area are able to deal with it to
everyone's satisfaction.
  -- Things That Never Happen in "Star Trek" #13

@p5pRT
Copy link
Author

p5pRT commented May 12, 2005

From RandyS@ThePierianSpring.org

Dave Mitchell wrote​:

On Wed, May 11, 2005 at 02​:14​:28PM -0400, Ronald J Kimball wrote​:

Of course split('') is equivalent to split(//), just as split('a|b') is
equivalent to split(/a|b/).

And of course split(/a\b/) is *not* equivalent to split("a\b"), so it's
probably bad form to encourage people to think that regex literals and
string literals are always equivalent, and hence bad to show '' and // as
identical (even if they happen to be in this case).

Similar discussion here several years ago set me against ever using
string literals in split because it's less prone to mistakes like that
above and because it's much clearer in intent. I generally recommend to
others to do the same anytime I see string literals in split.

It would've been nice if split(undef,...) could have been used as a
synonym for split() and split(' ',...) so that string literals in split
could be avoided completely.

Randy.

@p5pRT
Copy link
Author

p5pRT commented May 12, 2005

From @rgs

Randy W. Sims wrote​:

Dave Mitchell wrote​:

On Wed, May 11, 2005 at 02​:14​:28PM -0400, Ronald J Kimball wrote​:

Of course split('') is equivalent to split(//), just as split('a|b') is
equivalent to split(/a|b/).

And of course split(/a\b/) is *not* equivalent to split("a\b"), so it's
probably bad form to encourage people to think that regex literals and
string literals are always equivalent, and hence bad to show '' and // as
identical (even if they happen to be in this case).

Similar discussion here several years ago set me against ever using
string literals in split because it's less prone to mistakes like that
above and because it's much clearer in intent. I generally recommend to
others to do the same anytime I see string literals in split.

It would've been nice if split(undef,...) could have been used as a
synonym for split() and split(' ',...) so that string literals in split
could be avoided completely.

I concur (and I even tried to add a warning about this. Too noisy, though)

I thus applied this :

==== //depot/perl/pod/perlfunc.pod#468 (text) ====

@​@​ -5077,15 +5077,14 @​@​

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

-Using the empty pattern C<//> specifically matches the null string, and is
-not be confused with the use of C<//> to mean "the last successful pattern
-match". So, for C<split>, the following are equivalent​:
+As a special case for C<split>, using the empty pattern C<//> specifically
+matches only the null string, and is not be confused with the regular use
+of C<//> to mean "the last successful pattern match". So, for C<split>,
+the following​:

  print join('​:', split(//, 'hi there'));

- print join('​:', split('', 'hi there'));
-
-and they produce the output 'h​:i​: :t​:h​:e​:r​:e'.
+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

@p5pRT p5pRT closed this as completed May 13, 2005
@p5pRT
Copy link
Author

p5pRT commented May 13, 2005

@smpeters - Status changed from 'open' to 'resolved'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant