Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

why do

print scalar split /:/, ':';

write

0

and not

1

?

Replies are listed 'Best First'.
Re: split empty (lead|trail)ing fields
by bart (Canon) on Sep 27, 2003 at 00:46 UTC
    Because split throws away empty trailing fields unless you tell it not to do so, by a third parameter.

    In your case, there is nothing but empty fields, so split throws it all away.

    This snippet:

    print scalar split /:/, ':', -1;
    keeps them all, and prints 2.
Re: split empty (lead|trail)ing fields
by Zaxo (Archbishop) on Sep 27, 2003 at 00:40 UTC

    Because split throws away the parts that matched unless you tell it to keep them,

    perl -e'print scalar split /(:)/, q(:)' 2
    It turns out that capturing forces one element before the separator. ':' is in the second array slot.

    After Compline,
    Zaxo

Re: split empty (lead|trail)ing fields
by welchavw (Pilgrim) on Sep 27, 2003 at 02:24 UTC

    Hi,

    "perldoc -f split" gives...

    In scalar context, returns the number of fields found and splits into the @_ array. Use of split in scalar context is deprecated, however, because it clobbers your subroutine arguments.

    So..."0" is the number of fields found and your particular usage is deprecated (at least as of 5.8.0), which you may or may not care about. Compare the following code just for kicks.

    perl -e '$p = "::"; @m = ($p =~ /(:)/g); print scalar @m, "\n"'

    ,welchavw

      i think u didnt get the point: i was just counting the splitted elemets

      my @our_arguments_are_safe =split /:/, ':'; print scalar @our_arguments_are_safe;

      is it ok?

      $p = "::"; @m = ($p =~ /(:)/g);

      mm, why now do u count separators?

      the 'no-code' question was:
      why do split consider all empty fields as trailing when he find a list of two?

        I think the answer to your question is in this paragraph of the docs.

        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 beginning (or end) of the string does not produce an empty field.

        I say I think, because, even with the example given, I find it quite difficult to interpret that sentence. Especially in the light of a couple of other sentances

        By default, empty leading fields are preserved, and empty trailing ones are deleted.

        and

        If LIMIT is unspecified or zero, trailing null fields are stripped

        and

        Note that splitting an EXPR that evaluates to the empty string always returns the empty list, regardless of the LIMIT specified.

        Not at all clear to me:)


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
        If I understand your problem, I can solve it! Of course, the same can be said for you.