Ah. Ok. I can see where that might be confusing. Let's take a look at the section
Code: Select all
8. if tFound is 0 then
9. exit repeat
10. else
11. add tFound to tOffset
12. put tOffset & space after field "t2"
13. end if
First of all, if the word is not found (or there are no more occurrences of it) then the wordOffset function will return zero. That's line 8, and in that case we've found all the instances of the word in the string and we're done. So line 9 exits and we go on to do something significant with our results in hand.
If we got something other than a zero then we've encountered a new instance of the word. Parameter 3 of the wordOffset function is an offset from the starting point of our search (think of it as the number of words to ignore before we start searching). The result of the wordOffset function is relative to the offset parameter. So in our case if we say
put wordoffset("lazy", "the quick brown fox jumped over the lazy dog the other day")
or
put wordoffset("lazy", "the quick brown fox jumped over the lazy dog the other day", 0)
we'll get an answer of 8. But if we say
put wordoffset("lazy", "the quick brown fox jumped over the lazy dog the other day", 3)
we'll get an answer of 4 (the fourthword after word 3). So we need to add the result (tFound) to the offset in order to get the absolute offset from the beginning of the string and determine that we got a hit on the seventh word. If we didn't add it we'd get
put wordoffset("the", "the quick brown fox jumped over the lazy dog the other day". 0) = 1
then we put 1 into the offset and get
put wordoffset("the", "the quick brown fox jumped over the lazy dog the other day". 1) = 6
and if we don't add the offset and just use the result we get
put wordoffset("the", "the quick brown fox jumped over the lazy dog the other day". 6) = 1
and we'll keep going around and never get the tenth instance.
Hope this makes sense - it's been a long day/week/whatever. I need me some weekend.