in reply to Why does split on /./ not split the string?

split returns only the string segments between, but not including the matching chars. For example:

my @list = split(/;/, 'one;two;three'); # @list is now ('one', 'two', 'three');

Since /./ matches on any char, what you end up with is essentially emptiness. The lookahead works because it is looking ahead, not "matching" in the same sense as /./. FYI: split(q//, $string); will split the string into chars, and is a little easier to understand (and possibly faster) than using /(?=.)/.

<-radiant.matrix->
A collection of thoughts and links from the minds of geeks
The Code that can be seen is not the true Code
"In any sufficiently large group of people, most are idiots" - Kaa's Law

Replies are listed 'Best First'.
Re^2: Why does split on /./ not split the string?
by QM (Parson) on Dec 07, 2005 at 19:59 UTC
    split returns only the string segments between, but not including the matching chars.
    <nit>

    Uhm, then what does this do?

    my @list = split /([\W]+)/, 'one;two--three!!!';
    Updated: Corrected '+' typo.

    </nit>

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

      Your pattern to split has capturing parentheses, which returns fields along w/ the delimiters; see perldoc -f split. Also, that [+] (mind you + here is just a plain plus sign) is useless as the string to be split lacks one.

      Try this ...

        I was trying to nitpick, but failed somewhat. The point is that split's behavior (wrt/ the OP) depends on the presence of capturing parens and the DWIMmery of split.

        -QM
        --
        Quantum Mechanics: The dreams stuff is made of