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

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

Thread Previous | Thread Next
From:
mark.a.biggar
Date:
May 13, 2005 02:37
Subject:
Re: split /(..)*/, 1234567890
Message ID:
051220051821.9124.42839EAD000A847F000023A422070009539D0E0909070DD20ED2059D0E03@comcast.net
No, it's not inconsistant.  Think about the simpler case split /a/,'aaaaa' which return a list of empty strings.  Now ask to keep the separators
split /(a), 'aaaaa' which will return ('', 'a', '', 'a', '', 'a', '', 'a, '', 'a').  Now look at 
split /(a)/, 'aaab' which returns ('', 'a', '', 'a', '', 'a', 'b'). not no empty string ebfore the 'b'.

In the case of split /(..)/, "12345678" all those pairs of digits are all spearators so again you get  empty strings aternating with digit pairs.  If the number of digits is odd the lat on isn't  a separator so it takes the place of the final empty string and there won;t be a empty string in the list before it, I.e,
split /(..)/, 12345 returns (''. '12', '', '34', '5');

This is another of those cases where the computer did exactly what you ask it to.

--
Mark Biggar
mark@biggar.org
mark.a.biggar@comcast.net
mbiggar@paypal.com


> Autrijus Tang wrote:
> > I don't know, I didn't invent that! :-)
> > 
> >     $ perl -le 'print join ",", split /(..)/, 123'
> >     ,12,3
> 
> Hmm,
> 
> perl -le 'print join ",", split /(..)/, 112233445566'
> ,11,,22,,33,,44,,55,,66
> 
> For longer strings it makes every other match an empt string.
> With the "Positions between chars" interpretation the above
> string is with '.' indication position:
> 
> .1.1.2.2.3.3.4.4.5.5.6.6.
> 0 1 2 3 4 5 6 7 8 9 1 1 1
>                      0 1 2
> 
> There are two matches each at 0, 2, 4, 6, 8 and 10.
> The empty match at the end seams to be skipped because
> position 12 is after the string? And for odd numbers of
> chars the before last position doesn't produce an empty
> match:
> perl -le 'print join ",", split /(..)/, 11223'
> ,11,,22,3
> 
> Am I the only one who finds that inconsistent?
> -- 
> TSa (Thomas SandlaƟ)
> 

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