Perl / perl5

🐪 The Perl programming language
https://dev.perl.org/perl5/
Other
1.85k stars 524 forks source link

[doc] perlfunc split: my @x = split(//, "abc", 4); # ("a", "b", "c") <-- wrong, should be ("a", "b", "c", "") #22150

Closed MarkB-L closed 2 weeks ago

MarkB-L commented 2 weeks ago

I think there is a mistake in the perlfunc documentation for split().

It currently says... my @x = split(//, "abc", 3); # ("a", "b", "c") my @x = split(//, "abc", 4); # ("a", "b", "c")

It should say my @x = split(//, "abc", 3); # ("a", "b", "c") my @x = split(//, "abc", 4); # ("a", "b", "c", "")

Grinnz commented 2 weeks ago

You're correct but this is probably an unfortunate example. It most likely intends to show that the LIMIT is a maximum, so for example split /x/, "axbxc", 4 will only produce three fields. But the empty split produces a 4th trailing field (but not, for example, a 5th with a limit of 5).

MarkB-L commented 2 weeks ago

I agree, but shouldn't the doc be changed to your example, or changed to:-my @x = split(//, "abc", 4); # ("a", "b", "c", "")my @x = split(//, "abc", 5); # ("a", "b", "c", "")

On Tuesday, April 16, 2024 at 05:58:24 PM GMT+1, Dan Book ***@***.***> wrote:  

You're correct but this is probably an unfortunate example. It most likely intends to show that the LIMIT is a maximum, so for example split /x/, "axbxc", 4 will only produce three fields. But the empty split produces a 4th trailing field (but not, for example, a 5th with a limit of 5).

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***>