Find a complete word and count the number of occurences.
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
- Posts: 56
- Joined: Sat Jun 20, 2009 2:41 pm
Find a complete word and count the number of occurences.
I'm trying new things and learning my way around RunRev...
Built a very simple app that has a large text box, small text box and a button.
I enter lots of text into the large text box, enter a word into the small text box and hit the button.
on mouseup I have this code:
on mouseUp
get field "SearchTerm" put it into tsearchterm
put the number of words in field "book" into field "numwords"
put the number of characters in the field "book" into the field "numchar"
if tsearchterm is in field "book"
then put "yes" into field "isinbox"
else put "nope" into field "isinbox"
end mouseUp
So far I can count the number of characters in the text box, count the number of words in the text box, and tell if my search term is in the field "book"...
Two things I want to do and haven't figured out yet...
1. Not only does it show me if the term I entered into the search box is in the field "book", it also finds the characters inside of words. For instance if I search for "do" in the search box, it will find "door", "dog", "don't", etc. I only want to find the complete word I entered into the search box...
2. Once I find the complete word, how can I count how many times I have found that word?
Thanks!
Built a very simple app that has a large text box, small text box and a button.
I enter lots of text into the large text box, enter a word into the small text box and hit the button.
on mouseup I have this code:
on mouseUp
get field "SearchTerm" put it into tsearchterm
put the number of words in field "book" into field "numwords"
put the number of characters in the field "book" into the field "numchar"
if tsearchterm is in field "book"
then put "yes" into field "isinbox"
else put "nope" into field "isinbox"
end mouseUp
So far I can count the number of characters in the text box, count the number of words in the text box, and tell if my search term is in the field "book"...
Two things I want to do and haven't figured out yet...
1. Not only does it show me if the term I entered into the search box is in the field "book", it also finds the characters inside of words. For instance if I search for "do" in the search box, it will find "door", "dog", "don't", etc. I only want to find the complete word I entered into the search box...
2. Once I find the complete word, how can I count how many times I have found that word?
Thanks!
-
- VIP Livecode Opensource Backer
- Posts: 977
- Joined: Sat Apr 08, 2006 7:47 am
- Contact:
Take a look at the wordOffset and wholeMatches entries in the Dictionary. These two together should allow you to reach your goal.
Jan Schenkel.
Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com
www.quartam.com
Hi TodayIsTheDay,
Does this work for you?
Best,
Mark
Does this work for you?
Code: Select all
function nrOfOccurrences theData,theSearchTerm
replace space with cr in theData
filter theData with "*theSearchTerm*"
return number of lines of theData
end nrOfOccurrences
Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
-
- Posts: 56
- Joined: Sat Jun 20, 2009 2:41 pm
Well, a little better... Mark I am still really green at this and couldn't get the function you gave me to work.
Jan, I did this:
It tells me the first place it finds the word (assuming it does) but that's it. If there word is there 20 times it won't tell me.
I realize there is nothing to tell it to loop (which I still don't get) or count.
I would like it to tell me the position and the number of times it finds it...?
I'm just trying to write simple scripts to help me understand how things work them I'm making notes and sticking it into a code scrapbook...
Is any of the code above out of order or could be done easier / better?
Jan, I did this:
Code: Select all
on mouseUp
get field "SearchTerm" put it into tsearchterm
put the number of words in field "book" into field "numwords"
put the number of characters in the field "book" into the field "numchar"
set wholematches to true
if tsearchterm is in field "book"
then put "yes" into field "isinbox"
else put "nope" into field "isinbox"
put wordOffset(field "searchterm", field "book") into pos
answer "Found it in position" && pos
end mouseUp
I realize there is nothing to tell it to loop (which I still don't get) or count.
I would like it to tell me the position and the number of times it finds it...?
I'm just trying to write simple scripts to help me understand how things work them I'm making notes and sticking it into a code scrapbook...
Is any of the code above out of order or could be done easier / better?
-
- VIP Livecode Opensource Backer
- Posts: 977
- Joined: Sat Apr 08, 2006 7:47 am
- Contact:
Assuming there's a field 1, put the following script into a button:
I declared the 'sSearchTerm' variable as a local outisde the mouseUp handler, so it will propose the same search term the next time you click the button; the important bit here is the use of the third parameter for the wordOffset function to tell it where to start searching.
Just run it with the debugger to see what you get back from each call of the function.
HTH,
Jan Schenkel.
Code: Select all
local sSearchTerm
on mouseUp
local tWordOffset, tCurrentOffset, tWordOffsetList
--
ask "Which word should I find the occurences of?" with sSearchTerm
if the result is "Cancel" then exit mouseUp
put it into sSearchTerm
set the wholeMatches to true
put wordOffset(sSearchTerm, field 1) into tWordOffset
put tWordOffset into tCurrentOffset
repeat until tWordOffset is 0
put tCurrentOffset & comma after tWordOffsetList
put wordOffset(sSearchTerm, field 1, tCurrentOffset) into tWordOffset
add tWordOffset to tCurrentOffset
end repeat
delete char -1 of tWordOffsetList
answer "Found '" & sSearchTerm & "'" && the number of items in tWordOffsetList && "times:" & return & tWordOffsetList
end mouseUp
Just run it with the debugger to see what you get back from each call of the function.
HTH,
Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com
www.quartam.com
-
- Posts: 179
- Joined: Sat Apr 08, 2006 11:08 pm
- Contact:
Or a really simple approach:
Best,
Mark Smith
Code: Select all
on mouseUp
get field "SearchTerm" put it into tsearchterm
put the number of words in field "book" into field "numwords"
put the number of characters in the field "book" into the field "numchar"
set wholematches to true
if tsearchterm is in field "book"
then put "yes" into field "isinbox"
else put "nope" into field "isinbox"
put wordOffset(field "searchterm", field "book") into pos
answer "Found it in position" && pos
put 0 into hits
repeat for each word w in fld "book"
if w is tSearchTerm then add 1 to hits
end repeat
end mouseUp
Best,
Mark Smith
-
- Posts: 56
- Joined: Sat Jun 20, 2009 2:41 pm