split does not return empty elements

Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
Why do these not all return bbb?
$ perl -e '$a=" "; print map "b" split / /, $a;'
<<nothing>>
$ perl -e '$a=",,"; print map "b" split /,/, $a;'
<<nothing>>
$ perl -e '$a=" a"; print map "b" split / /, $a;'
bbb
$ perl -e '$a=",,a"; print map "b" split /,/, $a;'
bbb
I would have expected split to return an array with 3 elements in all cases.
$ perl -V
Summary of my perl5 (revision 5 version 24 subversion 1) configuration:
perl
migrated from unix.stackexchange.com Jul 20 at 3:57
This question came from our site for users of Linux, FreeBSD and other Un*x-like operating systems.
add a comment |Â
up vote
1
down vote
favorite
Why do these not all return bbb?
$ perl -e '$a=" "; print map "b" split / /, $a;'
<<nothing>>
$ perl -e '$a=",,"; print map "b" split /,/, $a;'
<<nothing>>
$ perl -e '$a=" a"; print map "b" split / /, $a;'
bbb
$ perl -e '$a=",,a"; print map "b" split /,/, $a;'
bbb
I would have expected split to return an array with 3 elements in all cases.
$ perl -V
Summary of my perl5 (revision 5 version 24 subversion 1) configuration:
perl
migrated from unix.stackexchange.com Jul 20 at 3:57
This question came from our site for users of Linux, FreeBSD and other Un*x-like operating systems.
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Why do these not all return bbb?
$ perl -e '$a=" "; print map "b" split / /, $a;'
<<nothing>>
$ perl -e '$a=",,"; print map "b" split /,/, $a;'
<<nothing>>
$ perl -e '$a=" a"; print map "b" split / /, $a;'
bbb
$ perl -e '$a=",,a"; print map "b" split /,/, $a;'
bbb
I would have expected split to return an array with 3 elements in all cases.
$ perl -V
Summary of my perl5 (revision 5 version 24 subversion 1) configuration:
perl
Why do these not all return bbb?
$ perl -e '$a=" "; print map "b" split / /, $a;'
<<nothing>>
$ perl -e '$a=",,"; print map "b" split /,/, $a;'
<<nothing>>
$ perl -e '$a=" a"; print map "b" split / /, $a;'
bbb
$ perl -e '$a=",,a"; print map "b" split /,/, $a;'
bbb
I would have expected split to return an array with 3 elements in all cases.
$ perl -V
Summary of my perl5 (revision 5 version 24 subversion 1) configuration:
perl
asked Jul 20 at 3:49
Ole Tange
17.6k35165
17.6k35165
migrated from unix.stackexchange.com Jul 20 at 3:57
This question came from our site for users of Linux, FreeBSD and other Un*x-like operating systems.
migrated from unix.stackexchange.com Jul 20 at 3:57
This question came from our site for users of Linux, FreeBSD and other Un*x-like operating systems.
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
9
down vote
accepted
split's third parameter says how many elements to produce:
split /PATTERN/,EXPR,LIMIT
...
IfLIMITis negative, it is treated as if it were instead arbitrarily large; as many fields as possible are produced.
If
LIMITis omitted (or, equivalently, zero), then it is usually treated as if it were instead negative but with the exception that trailing empty fields are stripped (empty leading fields are always preserved); if all fields are empty, then all fields are considered to be trailing (and are thus stripped in this case).
It defaults to 0, which means as many as possible but leaving off any trailing empty elements.
You can pass -1 as the third argument to split to suppress this behavior.
Wow thanks, did not not knowsplitcould take a third argument
â beasy
Jul 21 at 13:50
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
9
down vote
accepted
split's third parameter says how many elements to produce:
split /PATTERN/,EXPR,LIMIT
...
IfLIMITis negative, it is treated as if it were instead arbitrarily large; as many fields as possible are produced.
If
LIMITis omitted (or, equivalently, zero), then it is usually treated as if it were instead negative but with the exception that trailing empty fields are stripped (empty leading fields are always preserved); if all fields are empty, then all fields are considered to be trailing (and are thus stripped in this case).
It defaults to 0, which means as many as possible but leaving off any trailing empty elements.
You can pass -1 as the third argument to split to suppress this behavior.
Wow thanks, did not not knowsplitcould take a third argument
â beasy
Jul 21 at 13:50
add a comment |Â
up vote
9
down vote
accepted
split's third parameter says how many elements to produce:
split /PATTERN/,EXPR,LIMIT
...
IfLIMITis negative, it is treated as if it were instead arbitrarily large; as many fields as possible are produced.
If
LIMITis omitted (or, equivalently, zero), then it is usually treated as if it were instead negative but with the exception that trailing empty fields are stripped (empty leading fields are always preserved); if all fields are empty, then all fields are considered to be trailing (and are thus stripped in this case).
It defaults to 0, which means as many as possible but leaving off any trailing empty elements.
You can pass -1 as the third argument to split to suppress this behavior.
Wow thanks, did not not knowsplitcould take a third argument
â beasy
Jul 21 at 13:50
add a comment |Â
up vote
9
down vote
accepted
up vote
9
down vote
accepted
split's third parameter says how many elements to produce:
split /PATTERN/,EXPR,LIMIT
...
IfLIMITis negative, it is treated as if it were instead arbitrarily large; as many fields as possible are produced.
If
LIMITis omitted (or, equivalently, zero), then it is usually treated as if it were instead negative but with the exception that trailing empty fields are stripped (empty leading fields are always preserved); if all fields are empty, then all fields are considered to be trailing (and are thus stripped in this case).
It defaults to 0, which means as many as possible but leaving off any trailing empty elements.
You can pass -1 as the third argument to split to suppress this behavior.
split's third parameter says how many elements to produce:
split /PATTERN/,EXPR,LIMIT
...
IfLIMITis negative, it is treated as if it were instead arbitrarily large; as many fields as possible are produced.
If
LIMITis omitted (or, equivalently, zero), then it is usually treated as if it were instead negative but with the exception that trailing empty fields are stripped (empty leading fields are always preserved); if all fields are empty, then all fields are considered to be trailing (and are thus stripped in this case).
It defaults to 0, which means as many as possible but leaving off any trailing empty elements.
You can pass -1 as the third argument to split to suppress this behavior.
edited Jul 20 at 14:47
answered Jul 20 at 6:28
ysth
76.4k390183
76.4k390183
Wow thanks, did not not knowsplitcould take a third argument
â beasy
Jul 21 at 13:50
add a comment |Â
Wow thanks, did not not knowsplitcould take a third argument
â beasy
Jul 21 at 13:50
Wow thanks, did not not know
split could take a third argumentâ beasy
Jul 21 at 13:50
Wow thanks, did not not know
split could take a third argumentâ beasy
Jul 21 at 13:50
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51434850%2fsplit-does-not-return-empty-elements%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password