Returning a list?
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
Returning a list?
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,
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,
-
- VIP Livecode Opensource Backer
- Posts: 10049
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
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
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
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?
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?
-
- VIP Livecode Opensource Backer
- Posts: 10049
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
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:peter.s wrote:I don't understand it but it works!
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
^
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
|--- -- ^
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
|--- -- ^
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.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
-
- VIP Livecode Opensource Backer
- Posts: 10049
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
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.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?
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
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn