split does not return empty elements

The name of the pictureThe name of the pictureThe name of the pictureClash 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:






share|improve this question











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.


















    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:






    share|improve this question











    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.
















      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:






      share|improve this question











      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:








      share|improve this question










      share|improve this question




      share|improve this question









      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.






















          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



          ...
          If LIMIT is negative, it is treated as if it were instead arbitrarily large; as many fields as possible are produced.



          If LIMIT is 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.






          share|improve this answer























          • Wow thanks, did not not know split could take a third argument
            – beasy
            Jul 21 at 13:50











          Your Answer





          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          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






























          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



          ...
          If LIMIT is negative, it is treated as if it were instead arbitrarily large; as many fields as possible are produced.



          If LIMIT is 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.






          share|improve this answer























          • Wow thanks, did not not know split could take a third argument
            – beasy
            Jul 21 at 13:50















          up vote
          9
          down vote



          accepted










          split's third parameter says how many elements to produce:




          split /PATTERN/,EXPR,LIMIT



          ...
          If LIMIT is negative, it is treated as if it were instead arbitrarily large; as many fields as possible are produced.



          If LIMIT is 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.






          share|improve this answer























          • Wow thanks, did not not know split could take a third argument
            – beasy
            Jul 21 at 13:50













          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



          ...
          If LIMIT is negative, it is treated as if it were instead arbitrarily large; as many fields as possible are produced.



          If LIMIT is 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.






          share|improve this answer















          split's third parameter says how many elements to produce:




          split /PATTERN/,EXPR,LIMIT



          ...
          If LIMIT is negative, it is treated as if it were instead arbitrarily large; as many fields as possible are produced.



          If LIMIT is 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.







          share|improve this answer















          share|improve this answer



          share|improve this answer








          edited Jul 20 at 14:47


























          answered Jul 20 at 6:28









          ysth

          76.4k390183




          76.4k390183











          • 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
















          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













           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          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













































































          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Bahrain

          Postfix configuration issue with fips on centos 7; mailgun relay