develooper Front page | perl.perl6.language | Postings from September 2008

RE: Split with negative limits, and other weirdnesses

From:
Kealey, Martin, ihug-NZ
Date:
September 30, 2008 19:16
Subject:
RE: Split with negative limits, and other weirdnesses
Message ID:
732F909E6B12D549BACC18BB5E68945F02A03A5D@vfnz-ms1.vf-nz.internal.vodafone.com

Hmmm, my understanding was that it stopped *splitting* after the limit, but it doesn't stop "consuming" the source; rather the entire remainder is returned as the last item in the list, even if it contains the delimiter. A bit like this:

	sub split($pat, $src, $limit) {
		@r = split($pat, $src);
		return @r[0..$limit-2], join($pat, @r[$limit-1..*]);
	} 

except of course it works where $pat isn't a string literal, and does sensible things if $limit is 0 or 1, and is implemented more efficiently.

-Martin

> -----Original Message-----
> From: Mark J. Reed [mailto:markjreed@gmail.com] 
> Sent: Monday, September 29, 2008 8:44 AM
> To: Chris Davaz
> Cc: Carl Mäsak; Perl6
> Subject: Re: Split with negative limits, and other weirdnesses
> 
> On Sun, Sep 28, 2008 at 11:40 AM, Chris Davaz 
> <cdavaz@gmail.com> wrote:
> > Ok, so 0 returns the empty list and -1 violates the 
> signature? In PIR
> > can we have such signatures that put a constraint on the range of
> > values for a given parameter?
> 
> Maybe this has already been proposed and rejected, but why not simply
> define the limit parameter such that
> 
> split($pat, $src, $limit)
> 
> is equivalent, result-wise, to
> 
> split($pat, $src)[0 .. $limit - 1]
> 
> ?  Of course, the limit-parameter case might be able to avoid some
> work compared to the second, which might not always be able to do its
> job lazily, but the return value would be the same.  Then you don't
> have to come up with separate edge case rules for two different
> constructs... the one follows logically from the other.
> 
> $limit is 0?  @foo[0..-1] is the empty list. Check.
> 
> @foo[0..-2] is also the empty list.  So negative parameters 
> don't need to Fail..
> 
> -- 
> Mark J. Reed <markjreed@gmail.com>
> 

-----------------------------------------------------------------------------------------------
Have you seen our website?.... http://www.vodafone.co.nz

Manage Your Account, check your Vodafone Mail and send web2TXT online: http://www.vodafone.co.nz/myvodafone

CAUTION: This correspondence is confidential and intended for the named recipient(s) only.
If you are not the named recipient and receive this correspondence in error, you must not copy,
distribute or take any action in reliance on it and you should delete it from your system and
notify the sender immediately.  Thank you.

Unless otherwise stated, any views or opinions expressed are solely those of the author and do
not represent those of Vodafone New Zealand Limited.

Vodafone New Zealand Limited
20 Viaduct Harbour Avenue, Private Bag 92161, Auckland 1030
Telephone + 64 9 355 2000
Facsimile + 64 9 355 2001



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