From Waclaw.Marcin.Kusnierczyk at idi.ntnu.no  Thu May 28 16:05:28 2009
From: Waclaw.Marcin.Kusnierczyk at idi.ntnu.no (Wacek Kusnierczyk)
Date: Thu, 28 May 2009 16:05:28 +0200
Subject: [Rd] [R]   split strings
In-Reply-To: <4A1E96A9.6080802@idi.ntnu.no>
References: <BAY130-W260FD8C24AA6B865027A0BC3520@phx.gbl>	<4A1BF2AD.7040902@idi.ntnu.no>	<BAY130-W2070E9FB3B7C8A08D2A64BC3520@phx.gbl>	<4A1C501B.8020103@idi.ntnu.no>	<971536df0905261340q2a1df85av7a272f30b5230b87@mail.gmail.com>	<4A1CDEE6.40104@cybaea.com><4A1CF4CE.8040306@idi.ntnu.no>	<4A1E83D6.1020602@idi.ntnu.no>	<77EB52C6DD32BA4D87471DCD70C8D700013DBE67@NA-PA-VBE03.na.tibco.com>
	<4A1E96A9.6080802@idi.ntnu.no>
Message-ID: <4A1E9A28.6070206@idi.ntnu.no>

Wacek Kusnierczyk wrote:
> William Dunlap wrote:
>   
>> Would your patched code affect the following
>> use of regexpr's output as input to substr, to
>> pull out the matched text from the string?
>>    > x<-c("ooo","good food","bad")
>>    > r<-regexpr("o+", x)
>>    > substring(x,r,attr(r,"match.length")+r-1)
>>    [1] "ooo" "oo"  ""   
>>   
>>     
>
> no; same output
>
>   
>>    > substr(x,r,attr(r,"match.length")+r-1)
>>    [1] "ooo" "oo"  ""   
>>   
>>     
>
> no; same output
>
>   
>>    > r
>>    [1]  1  2 -1
>>    attr(,"match.length")
>>    [1]  3  2 -1
>>    > attr(r,"match.length")+r-1
>>    [1]  3  3 -3
>>    attr(,"match.length")
>>    [1]  3  2 -1
>>   
>>     
>
> for the positive indices there is no change, as you might expect.
>
> if i understand your concern, the issue is that regexpr returns -1 (with
> the corresponding attribute -1) where there is no match.  in this case,
> you expect "" as the substring. 
>
> if there is no match, we have:
>
>     start = r = -1 (the start you index provide)
>     stop = attr(r) + r - 1 = -1 + -1 -1 = -3 (the stop index you provide)
>
> for a string of length n, my patch computes the final indices as follows:
>
>     start' = n + start - 1
>     stop' = n + stop - 1
>
> whatever the value of n, stop' - start' = stop - start = -3 - 1 = -4. 
>   

except for that stop - start = -3 - -1 = -2, but that's still negative,
i.e., stop' < start'.
silly me, sorry.

vQ

> that is, stop' < start', hence an empty string is returned, by virtue of
> the original code.  (see the sources for details.)
>
> does this answer your question?
>
>



[Rd] [R] split strings
Wacek Kusnierczyk