Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Why does split on /./ not split the string?

by tphyahoo (Vicar)
on Dec 07, 2005 at 09:40 UTC ( [id://514762]=perlquestion: print w/replies, xml ) Need Help??

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

use strict; use warnings; #why does a split on a zero-length lookahead split the string... print "lookahead:\n"; my $string = 'abcde'; for ( split ( /(?=.)/, $string ) ) { print "$_\n"; } #but a split on just . does nothing? print "just .:\n"; for ( split ( /./, $string ) ) { print "$_\n"; }
UPDATE: Thanks months. /me whaps self on head...

Replies are listed 'Best First'.
Re: Why does split on /./ not split the string?
by GrandFather (Saint) on Dec 07, 2005 at 09:46 UTC

    It does. However because the . matches anything, the result may not be what you expect: all the chars are delimiter chars so no fields are found.


    DWIM is Perl's answer to Gödel

      Just to expand on this theme a bit, you can get those empty fields returned if you really want them, by passing -1 as the third argument to split: split /./,'abcde',-1 will dutifully return six empty strings. (Cases with values other than -1 are left as an exercise to the extremely bored.) Of course, there are probably faster ways to count the characters in your string, but that's a side issue. :-)



      If God had meant us to fly, he would *never* have given us the railroads.
          --Michael Flanders

Re: Why does split on /./ not split the string?
by TedPride (Priest) on Dec 07, 2005 at 09:46 UTC
    You might be interested to know that splitting with an empty regex // works fine. I use it all the time for turning strings into sequences of characters.
Re: Why does split on /./ not split the string?
by radiantmatrix (Parson) on Dec 07, 2005 at 15:37 UTC

    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
      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 ...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://514762]
Approved by Corion
Front-paged by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-04-25 18:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found