Newbie3

J. Landman Gay jacque at hyperactivesw.com
Mon Dec 8 13:38:18 EST 2008


dunbarx wrote:

> The
> lineOffset function only returns the first instance, so additional
> processing is required to get all of them.

That's why the last parameter is important; it allows you to search for 
an offset starting at a particular location. If you omit the last 
parameter, the search always starts at 1.

So this:

   get lineoffset(var,"ab")

will consistently return the first instance in the text variable. You 
need to set up a "skip" parameter:

   put 0 into skip
   repeat
    put lineoffset("ab",var,skip) into tResult
    if tResult = 0 then exit repeat
    put tResult & comma after tList
    add tResult to skip -- start next search at last found location
   end repeat

> So, wow me with Rev's
> arsenal. How to do it without a repeat loop? Find all "ab" in:
> 
> abc def abc
> 
> I want "1,3" back at me, or somesuch.

You need the repeat loop above, but it is extremely fast. You can 
process many thousands of lines in under a second, usually. The above 
gives you what you want, provided you are looking for the exact string 
match for "ab". You could also get chunk info instead:

   put fld 1 into var
   put 0 into skip
   repeat
     put lineoffset("ab",var,skip) into tLine
     if tLine = 0 then exit repeat
     add tLine to skip
     get matchChunk(line skip of var,"(ab)",tMatchStart,tMatchEnd)
     put tMatchStart && tMatchEnd & cr after tList
   end repeat

This gives the start and end position of the string you are searching 
for, within the line you are looking at. So if you get "1 3" in the 
match, then the chunk refered to would be "char 1 to 3 of line <skip> of 
fld 1".

If you want a regex search instead of an exact match for "ab" then you'd 
  set up a filter first and then run a variation of the above repeat on 
the results of your filtered text. Something like this:

   put fld 1 into var
   filter var with "a*b*"
   repeat for each line tLine in var
     get matchChunk(tLine,"(a
     put tMatchStart && tMatchEnd & cr after tList
   end repeat

Or you could use matchtext instead to extract the exact text.

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



More information about the Use-livecode mailing list