Page 1 of 1

Returning a list?

Posted: Wed Sep 24, 2008 1:31 pm
by peter.s
Hi,

Lets say I want to make a list of places where a particular word appears in a body of text.

I have tried the offset function with some success, but it returns only the first word it comes across, not all in the text. So:

get wordoffset ("A", "A word is a word is a word") -- returns 1

But I want a return of 1,4,7

I know this is a simple question, but can someone please explain how to do this?

Cheers,

Posted: Wed Sep 24, 2008 4:24 pm
by FourthWorld
Each of the offset functions (offset, wordoffset, lineoffset, itemoffset) allow an optional third argument which is the starting point for the search. You can then use it in a loop, incrementing this starting point argument by adding the value of the last offset to it, so it starts each iteration from the word after the last one found:

Code: Select all

on mouseUp
  put AllWordOffsets("A", "A word is a word is a word")
end mouseUp


function AllWordOffsets pSearchString, pText
  put 0 into tStart
  put empty into tReturnVal
  repeat
    get wordoffset(pSearchString, pText, tStart)
    if it = 0 then exit repeat
    add it to tStart
    put tStart &"," after tReturnVal
  end repeat
  delete last char of tReturnVal -- trailing comma
  --
  return tReturnVal
end AllWordOffsets

Posted: Wed Sep 24, 2008 4:39 pm
by peter.s
Hi there Richard,

I don't understand it but it works!

Thank you so much - I have been going around in circles for hours. Now I will study this script and hopefully some of it will stick in my head for future use.

Cheers,

Peter

Posted: Wed Sep 24, 2008 5:38 pm
by peter.s
Hi again,

Now I'm having trouble using this data. Once I have identified the location of these words in the text, I want to shift them. But my attempts don't work:

put fld "Field" into tVar
get AllWordOffsets ("word", tVar)
set the textshift of it in tVar to 4
put tVar into fld "SecondField"

What am I doing wrong?

Posted: Wed Sep 24, 2008 6:36 pm
by FourthWorld
peter.s wrote:I don't understand it but it works!
Code is good, but understanding is better. I have to admit I scratched my head a lot when I first encountered the start argument, so let me see if I can save some of your hair:

The first time through the loop, tStart is zero, so the search goes like this:

Code: Select all

A word is a word is a word
^
Offset: 1

We put that in our list of return values in tReturnVal.

The second time through, we add the first offset so now we start past that:

Code: Select all

A word is a word is a word
  |--- -- ^
 
Offset: 3

We add that to tStart to find the total offset thus far, 4, and add that to our list of return values.

Adding this to tStart again, the next search is:

Code: Select all

A word is a word is a word
            |--- -- ^
 
Offset: 3

We add that to tStart, bringing us to the total offset thus far, 7, and once again store that in our list of return values.

Each time through the loop we're checking if the offset is zero, meaning no more left to find. After that third one there are no more, so it exits and returns the list of offsets we've been storing in tReturnVal.

Hope that helps clarify it.

Posted: Wed Sep 24, 2008 6:43 pm
by FourthWorld
peter.s wrote:Hi again,

Now I'm having trouble using this data. Once I have identified the location of these words in the text, I want to shift them. But my attempts don't work:

put fld "Field" into tVar
get AllWordOffsets ("word", tVar)
set the textshift of it in tVar to 4
put tVar into fld "SecondField"

What am I doing wrong?
The textShift is a property of a chunk of text in a field, but your third line above attempts to apply it to a list of numbers returned by AllWordOffsets.

Maybe try something like this:

Code: Select all

lock screen
put AllWordOffsets ("word", fld "Field") into tList
put fld "Field" into fld "SecondField"
repeat for each item tOffset in tList
  set the textshift of word tOffset of fld "SecondField" to 4
end repeat
unlock screen
I added the lockscreen just so it doesn't slow things down with each iteration while it's working.

Posted: Thu Sep 25, 2008 3:06 am
by peter.s
This one I do understand. I sort of had the logic, but not the language... maybe this is a good opportunity to learn how to write repeat loops properly!

Thank you again Richard for your help and also for the explanation of the other script - slowly it sinks in... I think.

Cheers,

Peter