offset broken?
J. Landman Gay
jacque at hyperactivesw.com
Sat Feb 22 16:49:36 EST 2014
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.
--
Jacqueline Landman Gay | jacque at hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com
More information about the use-livecode
mailing list