Something like charIndex, but in a variable...

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

Something like charIndex, but in a variable...

Malte Brill
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
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: Something like charIndex, but in a variable...

Walt Brown
Best I could do is:

*on* mouseUp

*local* tVar, tStart, tEnd

*put* fld "fData" into tVar

*get* matchChunk(tVar, "(" & word 8 of tVar & ")", tStart, tEnd)

*answer* tStart & comma && tEnd

*end* mouseUp

On Thu, Apr 30, 2015 at 4:07 AM, Malte Brill <[hidden email]> 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
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: Something like charIndex, but in a variable...

xtalkprogrammer
In reply to this post by Malte Brill
Hi Malte,

If using a field is the only solution, try the templateField.

--
Kind regards,

Mark Schonewille
Economy-x-Talk
Http://economy-x-talk.com

Share the clipboard of your computer over a local network with Clipboard Link http://clipboardlink.economy-x-talk.com


Op 30 apr. 2015 om 10:07 heeft Malte Brill <[hidden email]> het volgende geschreven:

> 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
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode

_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Kind regards,

Drs. Mark Schonewille

Economy-x-Talk Consultancy and Software Engineering
Homepage: http://economy-x-talk.com
Twitter: http://twitter.com/xtalkprogrammer
Facebook: http://facebook.com/LiveCode.Beginner
KvK: 50277553
Reply | Threaded
Open this post in threaded view
|

Re: Something like charIndex, but in a variable...

dunbarxx
How about this, assuming the chunk you want is the second word:



on mouseUp
   put fld 1 into temp
   answer offset(char 1 of word 2 of temp,temp)
   answer offset(char (the number of chars of word 2 of temp) of word 2 of temp,temp)
end mouseUp


Craig


-----Original Message-----
From: Mark Schonewille <[hidden email]>
To: How to use LiveCode <[hidden email]>
Sent: Thu, Apr 30, 2015 8:00 am
Subject: Re: Something like charIndex, but in a variable...


Hi Malte,

If using a field is the only solution, try the
templateField.

--
Kind regards,

Mark
Schonewille
Economy-x-Talk
Http://economy-x-talk.com

Share the clipboard of
your computer over a local network with Clipboard Link
http://clipboardlink.economy-x-talk.com


Op 30 apr. 2015 om 10:07 heeft Malte
Brill <[hidden email]> het volgende geschreven:

> 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
>
[hidden email]
> Please visit this url to subscribe, unsubscribe
and manage your subscription preferences:
>
http://lists.runrev.com/mailman/listinfo/use-livecode

_______________________________________________
use-livecode
mailing list
[hidden email]
Please visit this url to subscribe,
unsubscribe and manage your subscription
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
 
_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: Something like charIndex, but in a variable...

J. Landman Gay
In reply to this post by Malte Brill
Will the offset function work?

put the number of chars in line 1 to 14 of tVar into tCount
put offset(startChar, line 15 of tVar)  + tCount into tStart
put offset(endChar, line 15 of tVar)  + tCount into tEnd


On April 30, 2015 3:07:05 AM CDT, Malte Brill <[hidden email]> 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
>[hidden email]
>Please visit this url to subscribe, unsubscribe and manage your
>subscription preferences:
>http://lists.runrev.com/mailman/listinfo/use-livecode

--
Jacqueline Landman Gay         |     [hidden email]
HyperActive Software           |     http://www.hyperactivesw.com

_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: Something like charIndex, but in a variable...

dunbarxx
In reply to this post by Malte Brill
Just so. Too fast on keyboard, too slow in brain. How about this:

on mouseUp
   put fld 1 into temp
   get the number of chars of word 1 to 8 of temp -- first char
   answer it & "," & the number of chars of word 8 of temp + it
end mouseUp


Craig
Reply | Threaded
Open this post in threaded view
|

Re: Something like charIndex, but in a variable...

Ben Rubinstein
In reply to this post by Malte Brill
On 30/04/2015 09:07, Malte Brill wrote:
> 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“)

(unhelpful to Malte, sorry) - I hadn't noticed the introduction of charIndex.
  What a useful property.  It would be great if it could be extended to
arbitrary sources of value.

In the meantime, could one write an ugly function to calculate the start index
of a chunk by first getting the length of the preceeding chunks, then using
that value as the charsToSkip for a call to offset on the value of the chunk?

So a first approximation - it would need some better work parsing the chunk
expression to be robust -

function chunkIndex tFullChunkExpr, tValue
    local tChunkExpr, tPriorChunksExpr, tChunkValue, iConsumed, n, i

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

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

    -- find the start of each chunk in the expression, from major to minor
    repeat with i = number of lines in tFullChunkExpr down to 1
       put line i of tFullChunkExpr into tChunkExpr

       -- make an expression for the chunks prior to this one
       put tChunkExpr into tPriorChunksExpr
       put format("1 to %d", word 2 of tChunkExpr - 1) \
             into word 2 of tPriorChunksExpr

       -- get the length of the chunks prior to this one
       do format("put length(%s of tValue) into n", tPriorChunksExpr)

       -- get this chunk, and find the first instance
       -- of it after the length of the prior chunks
       do ("put" && tChunkExpr && "of tValue into tChunkValue")
       add offset(tChunkValue, tValue, n) + n to iConsumed

       -- repeat within this chunk
       put tChunkValue into tValue
    end repeat

    return iConsumed - 1
end chunkIndex


To be called as e.g.
    get chunkIndex("token 7 of line 15 of", myVar)

I suspect that this could be fooled if there was a lot of leading white space
(and a repetition of the target chunk prior to it), but that could probably be
dealt with by an additional check for offset of the 'priorChunkValue'.


Ben

_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: Something like charIndex, but in a variable...

pmbrig
In reply to this post by J. Landman Gay
Here's a function I use all the time, which may come in handy:


function offsets str, pContainer
   -- returns a comma-delimited list of all the offsets of str in pContainer
   -- returns 0 if not found
   -- note: offsets("xx","xxxxxx") returns "1,3,5" not "1,2,3,4,5"
   --     ie, overlapping offsets are not counted
   -- note: to get the last occurrence of a string in a container (often
useful)
   --     use "item -1 of offsets(...)"
   -- by Peter M. Brigham, [hidden email] — freeware

   if str is not in pContainer then return 0
   put 0 into startPoint
   repeat
      put offset(str,pContainer,startPoint) into thisOffset
      if thisOffset = 0 then exit repeat
      add thisOffset to startPoint
      put startPoint & comma after offsetList
      add length(str)-1 to startPoint
   end repeat
   return item 1 to -1 of offsetList -- delete trailing comma
end offsets


-- Peter

Peter M. Brigham
[hidden email]
http://home.comcast.net/~pmbrig


On Thu, Apr 30, 2015 at 10:48 AM, J. Landman Gay <[hidden email]>
wrote:

> Will the offset function work?
>
> put the number of chars in line 1 to 14 of tVar into tCount
> put offset(startChar, line 15 of tVar)  + tCount into tStart
> put offset(endChar, line 15 of tVar)  + tCount into tEnd
>
>
> On April 30, 2015 3:07:05 AM CDT, Malte Brill <[hidden email]>
> 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
> >[hidden email]
> >Please visit this url to subscribe, unsubscribe and manage your
> >subscription preferences:
> >http://lists.runrev.com/mailman/listinfo/use-livecode
>
> --
> Jacqueline Landman Gay         |     [hidden email]
> HyperActive Software           |     http://www.hyperactivesw.com
>
> _______________________________________________
> use-livecode mailing list
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: Something like charIndex, but in a variable...

Peter Haworth
In reply to this post by Ben Rubinstein
For those whose eyes glaze over when looking at regexp, it seems Jacque's
suggestion of the offset function making use of the chars to skip easily
solves this problem.  I don;t have the original post in front of me so
can't remember if the objective was to find the last occurrence of the
character or the first and last.  This finds both.

on mouseUp

   put zero into tSkip
   repeat until tOffset is zero
         put offset("b",tVar,tSkip) into tOffset
         if tOffset <>0 then
               if tSkip is zero then
                     put tOffset into tStart
               else
                     put tSkip+tOffset into tEnd
               end if
               add tOffset to tSkip
         end if
   end repeat

end mouseUp

No idea on performance.  I've often thought it would be great if there was
a way for offset to start at the end of a string and work backwards, maybe
a "direction" parameter.




Pete
lcSQL Software <http://www.lcsql.com>
Home of lcStackBrowser <http://www.lcsql.com/lcstackbrowser.html> and
SQLiteAdmin <http://www.lcsql.com/sqliteadmin.html>

On Thu, Apr 30, 2015 at 8:50 AM, Ben Rubinstein <[hidden email]> wrote:

> On 30/04/2015 09:07, Malte Brill wrote:
>
>> 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“)
>>
>
> (unhelpful to Malte, sorry) - I hadn't noticed the introduction of
> charIndex.  What a useful property.  It would be great if it could be
> extended to arbitrary sources of value.
>
> In the meantime, could one write an ugly function to calculate the start
> index of a chunk by first getting the length of the preceeding chunks, then
> using that value as the charsToSkip for a call to offset on the value of
> the chunk?
>
> So a first approximation - it would need some better work parsing the
> chunk expression to be robust -
>
> function chunkIndex tFullChunkExpr, tValue
>    local tChunkExpr, tPriorChunksExpr, tChunkValue, iConsumed, n, i
>
>    -- convert nested chunk expression into lines, minor to major
>    replace " of " with return in tFullChunkExpr
>    if last word of tFullChunkExpr = "of" then \
>         delete last word of tFullChunkExpr
>
>    -- accumulate start of each level of chunk
>    put 0 into iConsumed
>
>    -- find the start of each chunk in the expression, from major to minor
>    repeat with i = number of lines in tFullChunkExpr down to 1
>       put line i of tFullChunkExpr into tChunkExpr
>
>       -- make an expression for the chunks prior to this one
>       put tChunkExpr into tPriorChunksExpr
>       put format("1 to %d", word 2 of tChunkExpr - 1) \
>             into word 2 of tPriorChunksExpr
>
>       -- get the length of the chunks prior to this one
>       do format("put length(%s of tValue) into n", tPriorChunksExpr)
>
>       -- get this chunk, and find the first instance
>       -- of it after the length of the prior chunks
>       do ("put" && tChunkExpr && "of tValue into tChunkValue")
>       add offset(tChunkValue, tValue, n) + n to iConsumed
>
>       -- repeat within this chunk
>       put tChunkValue into tValue
>    end repeat
>
>    return iConsumed - 1
> end chunkIndex
>
>
> To be called as e.g.
>    get chunkIndex("token 7 of line 15 of", myVar)
>
> I suspect that this could be fooled if there was a lot of leading white
> space (and a repetition of the target chunk prior to it), but that could
> probably be dealt with by an additional check for offset of the
> 'priorChunkValue'.
>
>
> Ben
>
> _______________________________________________
> use-livecode mailing list
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: Something like charIndex, but in a variable...

Alex Tweedly-2
In reply to this post by Malte Brill
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
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode


_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: Something like charIndex, but in a variable...

Ben Rubinstein
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
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: Something like charIndex, but in a variable...

Alex Tweedly-2
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
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode


_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode