develooper Front page | perl.perl6.language | Postings from May 2005

Re: split /(..)*/, 1234567890

Thread Previous | Thread Next
From:
Aaron Sherman
Date:
May 12, 2005 09:59
Subject:
Re: split /(..)*/, 1234567890
Message ID:
1115917160.27930.209.camel@pps
On Thu, 2005-05-12 at 12:22, David Storrs wrote:
> On May 12, 2005, at 11:59 AM, Autrijus Tang wrote:
> > On Thu, May 12, 2005 at 04:53:06PM +0200, "TSa (Thomas Sandla´┐Ż)"  
> > wrote:
> >> Autrijus Tang wrote:
> >>
> >>>    pugs> split /(..)*/, 1234567890
> >>>    ('', '12', '34', '56', '78', '90')

> >> Why the empty string match at the start?

> > I don't know, I didn't invent that! :-)

> >     $ perl -le 'print join ",", split /(..)/, 123'
> >     ,12,3
> 
> This makes sense when I think about what split is doing, but it is  
> surprising at first glance.  Perhaps this should be included as an  
> example in the docs?

perldoc -f split says:

        "Splits a string into a list of strings and returns that list.
        By default, empty leading fields are preserved, and empty
        trailing ones are deleted [...] If PATTERN is also omitted,
        splits on whitespace (after skipping any leading whitespace).
        [...] Empty leading (or trailing) fields are produced when there
        are positive width matches at the beginning (or end) of the
        string [...] As a special case, specifying a PATTERN of space ('
        ') will split on white space just as "split" with no arguments
        does. Thus, "split(' ')" can be used to emulate awk's default
        behavior, whereas "split(/ /)" will give you as many null
        initial fields as there are leading spaces [...]"

And there you have it.

-- 
Aaron Sherman <ajs@ajs.com>
Senior Systems Engineer and Toolsmith
"It's the sound of a satellite saying, 'get me down!'" -Shriekback



Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About