to find the first numeric line?

J. Landman Gay jacque at hyperactivesw.com
Fri Feb 1 23:13:47 EST 2013


I'm glad you did this, it's interesting. I'm not sure about the binary 
results. Doesn't the repeat loop exit early if the search block contains 
a few short lines? I think you could get a block of text that might 
contain the first instance of a leading digit, but they wouldn't be 
checked because their total length would be less than kBrute.

Anyway, for lists with shorter lines the script would need to lower the 
kBrute variable to a smaller number, and then the timing might be 
different. 10,000 seems like a pretty large exit condition.

I could easily not be understanding what's going on though. These things 
warp my brain.


On 2/1/13 6:41 PM, Alex Tweedly wrote:
>
> I just can't resist a good benchmark / coding challenge.
>
...
>>
>> function f3 @pD
>>    local r, tLower, tUpper, tMid, c, t, temp
>>    constant kBrute = 10000
>>
>>    put 1 into tLower
>>    put (the number of chars in pD) + 1 into tUpper
>>
>>    repeat 10000 times -- more than enough :-)
>>       if tUpper - tLower < kBrute then exit repeat
>>       put (tUpper + tlower ) / 2 into tMid
>>       put char 1 of line 2 of (char tMid to tUpper of pD) into c
>>       if c is a number then
>>          put tMid into tUpper
>>       else
>>          put tMid into tLower
>>       end if
>>    end repeat
>>
>>    put char tLower to tUpper of pD into temp
>>    filter temp without "[0-9]*"
>>    if temp is empty then
>>       -- no lines start with numeric
>>       return "No lines"
>>    else
>>       put the number of lines in (char  1 to tLower of pD) into t
>>       add the number of lines in temp to t
>>       return t
>>    end if
>> end f3

-- 
Jacqueline Landman Gay         |     jacque at hyperactivesw.com
HyperActive Software           |     http://www.hyperactivesw.com




More information about the use-livecode mailing list