Find a complete word and count the number of occurences.

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
TodayIsTheDay
Posts: 56
Joined: Sat Jun 20, 2009 2:41 pm

Find a complete word and count the number of occurences.

Post by TodayIsTheDay » Thu Sep 10, 2009 3:24 am

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!

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Contact:

Post by Janschenkel » Thu Sep 10, 2009 5:54 am

Take a look at the wordOffset and wholeMatches entries in the Dictionary. These two together should allow you to reach your goal.

Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Thu Sep 10, 2009 8:16 am

Hi TodayIsTheDay,

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
Best,

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

TodayIsTheDay
Posts: 56
Joined: Sat Jun 20, 2009 2:41 pm

Post by TodayIsTheDay » Thu Sep 10, 2009 4:02 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:

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
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?

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Contact:

Post by Janschenkel » Thu Sep 10, 2009 7:43 pm

Assuming there's a field 1, put the following script into a button:

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
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.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

Mark Smith
Posts: 179
Joined: Sat Apr 08, 2006 11:08 pm
Contact:

Post by Mark Smith » Thu Sep 10, 2009 7:52 pm

Or a really simple approach:

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

TodayIsTheDay
Posts: 56
Joined: Sat Jun 20, 2009 2:41 pm

Post by TodayIsTheDay » Thu Sep 10, 2009 7:55 pm

Thank you that is perfect. That really helped me learn a couple of new things!

BTW, I had sent you an email a few days ago about looking at reports and some ideas on them...

Post Reply