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

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

Thread Previous
Jonathan Scott Duff
May 12, 2005 10:50
Re: split /(..)*/, 1234567890
Message ID:
On Thu, May 12, 2005 at 01:12:26PM -0400, Uri Guttman wrote:
> >>>>> "JSD" == Jonathan Scott Duff <> writes:
>   JSD> To bring this back to perl6, autrijus' original query was regarding
>   JSD> 	$ pugs -e 'say join ",", split /(..)*/, 1234567890'
>   JSD> which currently generates a list of ('','12','34','56','78','90')
>   JSD> In perl5 it would generate a list of ('','90') because only the last
>   JSD> pair of characters matched is kept (such is the nature of quantifiers
>   JSD> applied to capturing parens). But in perl6 quantified captures put all
>   JSD> of the matches into an array such that "abcdef" ~~ /(..)*/ will make
>   JSD> $0 = ['ab','cd','ef']. 
>   JSD> I think that the above split should generate a list like this:
>   JSD> 	('', [ '12','34','56','78','90'])
> i disagree. if you want complex tree results, use a rule.

Well ... we *are* using a rule; it just doesn't have a name.

So, would you advocate too that 

	my @a = "foofoofoobarbarbar" ~~ /(foo)+ (bar)+/;

should flatten? thus @a = ('foo','foo','foo','bar','bar','bar')
rather than (['foo','foo','foo'],['bar','bar','bar]) ?

This may have even been discussed before but we should probably make
the determination as to whether or not we keep the delimiters be
something other than the presence or absense of parentheses in the
pattern.  Perhaps the flattening/non-flattening behavior could be
modulated the same way.  Probably as a modifier to split

> split is for creating a single list of elements from a string. it is
> better keep split simple for it is commonly used in this domain.

I'll wager that splits with non-capturing patterns are far and away the
most common case. :-)

Jonathan Scott Duff

Thread Previous Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About