Something like charIndex, but in a variable...

Alex Tweedly alex at tweedly.org
Fri May 1 20:13:38 EDT 2015


You're right Ben - your general approach can be combined with my (simple 
minded) approach, and a few simple checks thrown in to give us a general 
function (reasonably, but not comprehensively, tested - up to things 
like "token 3 of word 2 to 4 of line 3 to 7" ...)

on mouseup
    local myChunk, myVal, tResult

    put the text of fld "fldIn" into myChunk
    put the text of fld "fldOut" into myVal
    put chunkIndex( myChunk, myVal ) into tResult
    put tResult &CR after msg
    do ("put char " && tResult && "of myVal &CR after msg")
end mouseup

function chunkIndex pFullChunkExpr, pValue
    local tChunkExpr, tPriorChunksExpr, tChunkValue, tConsumed, n, i

    -- convert nested chunk expression into lines, minor to major
    replace " of " with return in pFullChunkExpr
    if last word of pFullChunkExpr = "of" then \
          delete last word of pFullChunkExpr

    -- accumulate start of each level of chunk
    put 0 into tConsumed

    -- handle the 'major' parts
    local L, temp, tStart, tCommand, nextValue, tWhite
    repeat with i = the number of lines in pFullChunkExpr down to 2
       put line i of pFullChunkExpr into L
       put  word 2 of L into tStart
       if tStart is not a number then
          -- we have a malformed (or beyond our understanding) chun expr
          return "-1 to -1"
       end if

       -- count number of chars in previous part, and accumulate it
       put "put the number of chars in" && word 1 of L into tCommand
       put " 1 to " & (tStart-1) after tCommand
       put " of pValue into temp"  after tCommand
       do tCommand
       add temp to tConsumed

       -- then get the remainder part
       put "put" && word 1 of L && tStart into tCommand
       put " " & word 3 to -1 of L after tCommand
       put " of pValue into nextValue"  after tCommand
       do tCommand

       -- now account for white space between the stripped away part and 
the remaining part
       put offset(nextValue, pValue, temp) into tWhite
       add tWhite-1 to tConsumed

       put nextValue into pValue
    end repeat
    -- and then do the "minor" part

    put line 1 of pFullChunkExpr into L
    put  word 2 of L into tStart

    put "put the number of chars in" && word 1 of L into tCommand
    put " 1 to " & (tStart-1) after tCommand
    put " of pValue into temp"  after tCommand
    do tCommand
    add temp to tConsumed

    put "put" && L into tCommand
    put " of pValue into nextValue"  after tCommand
    do tCommand

    -- now account for white space between the preceding part and the 
interesting part
    put offset(nextValue, pValue, temp) into tWhite
    add tWhite to tConsumed

    put the number of chars in nextValue into temp

    put " to" && tConsumed+temp-1 after tConsumed

    return tConsumed
end chunkIndex

-- Alex.

On 01/05/2015 11:53, Ben Rubinstein wrote:
> Hi Alex,
>
> Yes that's that approach I was trying to make a relatively general 
> version of (see my response 16:50) to handle arbitrarily complex chunk 
> expressions - but it would still fail (in fact probably worse?) with 
> chunk 1.  But I think the principle is right.
>
> Ben
>
> On 30/04/2015 23:26, Alex Tweedly wrote:
>> Not quite so straightforward, but  you can do
>>
>> function getCharIndexByLineToken pVar, pLine, pToken
>>    local temp
>>    put the number of chars in line 1 to (pLine-1) of pVar into temp
>>    add 1 to temp   -- for the CR between lines 14 and 15 !!
>>    add the number of chars in token 1 to (pToken-1) of line 15 of 
>> pVar to temp
>>    return temp
>> end getCharIndexByLineToken
>>
>> Unfortunately you need different functions for token, word, etc. 
>> unless you
>> want to make it more complex and (probably) need to use "do" or "value"
>>
>> Hmmm - and maybe you need to watch that "add 1" in the case of pLine 
>> being 1 !!
>> or beyond the end ?!?
>>
>> -- Alex.
>>
>>
>>
>>
>> On 30/04/2015 09:07, Malte Brill wrote:
>>> Hi all,
>>>
>>> I need to find the start and end character of chuncks in a variable. 
>>> If we
>>> are in a field we can use charIndex for that. However, in a variable 
>>> I have
>>> no good idea on how to do something similar to
>>>
>>> get charIndex(token 7 of line 15 of field „myField“)
>>>
>>>
>>> Any ideas?
>>>
>>> All the best,
>>>
>>> Malte
>
>
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your 
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode





More information about the use-livecode mailing list