Up to now we only wanted to find a specific term once. But what if we have lots of occurrence of an item, and want to know the position of each of them?
For this, the offset functions all have an optional third parameter, which I can set to where the offset should start. let me show an example text:
abc
ab
c
So if I'd want to know where "c" is, but skip the first occurrence, I could say:
put lineoffset("c", myList,1) -- will be 2 for the above example
Now maybe you'd say it should be 3, and I'd agree with that. But offset is a bit special, as it factors your specified skipping value into its return value. Although the "c" is on line 3, lineoffset returns 2 if you specify a skip value of 1 (2 + 1 equals then the 3 we where actually looking for).
To get every item in a comma delimited list that contains "c", I made the following example code. Note how I use the value returned by the function to find the next "c" (within the repeat loop).
on mouseUp
put itemoffset("c", field 1) into theValue
put theValue & comma into theResult
repeat forever
put itemoffset("c", field 1,theValue) into newValue
if newValue = 0 then
-- all done!
exit repeat
end if
add newValue to theValue
put theValue & comma after theResult
end repeat
delete char -1 of theResult -- remove trailing comma
put theResult
end mouseUp
But frankly, all the return values, and parameters, and strange use of repeat forever make my head spin. That is why I normally use this following code, when I want to find every occurrence of an item. It does exactly the same as the code above, just in a completely different way.
on mouseUp
put itemoffset("c", field 1) into theValue
if theValue <> 0 then
repeat for each item currentItem in field 1
add one to myCounter
if currentItem contains "c" then -- with "wholematches": if currentItem is "c" then
put myCounter & comma after theResult
end if
end repeat
end if
delete char -1 of theResult
put theResult
end mouseUp