offset broken?

Richmond richmondmathewson at gmail.com
Sun Feb 23 04:57:57 EST 2014


On 22/02/14 23:49, J. Landman Gay wrote:
> On 2/22/14, 1:35 PM, Richmond wrote:
>> Sorry: Neanderthal question time:
>>
>> I cannot see anything about charsToSkip in the inbuilt (i.e. in the IDE)
>> documentation (6.6. dp1).
>
> It's the third parameter in the "offset" function. See "offset" in the 
> dictionary.
>
>> I set up a boring little stack with a fld "fORIGIN" conatinong "The
>> quick brown fox jumps over the lazy dog"
>>
>> and that was jolly nice,
>>
>> then I set up another fld called "fREZ"
>>
>> and a button with this script:
>>
>> on mouseUp
>>     put empty into fld "fREZ"
>>     put fld "fORIGIN" into ORIGIN
>>     put offset("o",ORIGIN) & ", " after fld "fREZ"
>>     put offset("o",ORIGIN) & ", " after fld "fREZ"
>>     put offset("o",ORIGIN) & ", " after fld "fREZ"
>> end mouseUp
>>
>>
>> in the hope I would get something like "13, 18, 27," in fld "fREZ"
>>
>> but all I got was "13, 13, 13"
>>
>> so I really cannot see what the utility of 'offset' is at all.
>
> Oh ye of little faith. That's what we've been talking about; every 
> instance of "offset" starts at the beginning of the string unless you 
> include the number of characters to skip as the third parameter.
>
> This is a much faster way to get all instances of an offset in a block 
> of text, exactly parallel to the differences between "repeat with x" 
> and "repeat for each". Like "for each," offset will read from the 
> current marked location rather than counting from the top during each 
> iteration, provided you include the third parameter. Your example 
> handler doesn't use the parameter, so of course you're getting the 
> same offset repeatedly.
>
> If there is a third parameter, then offset will start counting from 
> that point, and return the number of characters from that location in 
> the text to the next instance of the character. It's up to you to add 
> that number to the current "skip" location in order to keep the marker 
> moving along.
>
> This should do what you want:
>
> function getAllOffsets pData,pChar -- find all instances of a char
>   put 0 into tSkip -- start at beginning
>   repeat
>     put offset(pChar,pData,tSkip) into tNextOffset -- chars since skip 
> point
>     if tNextOffset = 0 then exit repeat -- no more found
>     add tNextOffset to tSkip -- move the marker
>     put tSkip & comma after tRes -- store it
>   end repeat
>   return tRes
> end getAllOffsets
>
> This should be much faster than your "repeat" example (which counts 
> characters from the top during each iteration) or a version that 
> deletes characters from the original data. Try some comparison timings 
> with a very large block of text.
>

  You AVOIDED commenting on this:

/I made another button with this script:
//
//  on mouseUp
//     put empty into fld "fREZ"
//     put 1 into INDEKS
//     repeat until char INDEKS of fld "fORIGIN" is empty
//     if char INDEKS of fld "fORIGIN" is "o" then
//       put INDEKS & ", " after fld "fREZ"
//    else
//       --do nix
//    end if
//    put (INDEKS + 1) into INDEKS
//    end repeat
//  end mouseUp
//
//  and that returned "13, 18, 27, 42"
//
//  a whole lot more useful./


No need to work out how far to hop!

Richmond.



More information about the use-livecode mailing list