in reply to Bug in split?

From split (emphasis mine):

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).
$ perl -E'say scalar( split( /\./, q{.}, -1 ));' 2 $ perl -E'say scalar( split( q{.}, q{.}, -1 ));' 2

Update: BTW, your two patterns are not identical:

$ perl -MData::Dump -e 'dd split( /\./, q{.x.} )' ("", "x") $ perl -MData::Dump -e 'dd split( q{.}, q{.x.} )' () $ perl -MData::Dump -e 'dd split( /\./, q{.x.}, -1 )' ("", "x", "") $ perl -MData::Dump -e 'dd split( q{.}, q{.x.}, -1 )' ("", "", "", "")

Replies are listed 'Best First'.
Re^2: Bug in split?
by shawnhcorey (Friar) on Sep 27, 2017 at 14:30 UTC

    In other words, it's backward. This is what you get when you inherit from othe languages. Sigh.

      "...what you get when you inherit from other languages..."

      My predecessors already pointed out that split works as designed. So just for curiosity: May i ask which other languages you mean?

      Regards, Karl

      «The Crux of the Biscuit is the Apostrophe»

      perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

        May i ask which other languages you mean?

        My guess would be the following quote from perldoc perl:

        [Perl] combines (in the author's opinion, anyway) some of the best features of sed, awk, and sh, making it familiar and easy to use for Unix users to whip up quick solutions to annoying problems.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)