Find function

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Rod Edwardson
Posts: 23
Joined: Sun Mar 03, 2013 1:09 am

Find function

Post by Rod Edwardson » Sun Mar 03, 2013 1:28 am

Hello all. have been playing with this for days and can't get it working in a smooth fashion. I have a text box that will contain a few paragraphs of text. I have a list of about 400 words and want to perform a find on each of these words with a buttons mouseUp. Once found I have changed the textColor of the foundText to red. I have it working with an individual word but cant get it to work on multiple words without adding 400 individual find commands. Any suggestions? I have

on mouseUp
repeat until found empty
find word "cat" in field "myField"
set the textColor of foundText to red
if the result is not empty then
exit to top
end if
end mouseUp


I want to find cat, dog, mouse etc. with an additional 400 words and turn the found words to red.

Thanks in advance!

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Find function

Post by sturgis » Sun Mar 03, 2013 2:07 am

First thing to note..

For a repeat block you need "repeat until condition"

and "end repeat" Gotta have the end repeat or there will be problems though i'm assuming its just a typo.

Then, if you have a list of words you can cycle through each word and mark them as needed.

Code: Select all

on mouseUp
   put "dog,horse,pig,geese,goose" into tWords
   repeat for each item tItem in tWords --repeat for each item (comma is default item delimiter)
      -- the repeat places each item into the temporary varaible tItem for use in the script 
      
      find empty -- make sure find is starting fresh
      put false into tExit -- var used to exit internal loop
      
      repeat until tExit -- if tExit is true then the loop will exit. 
         find words tItem in field 1 -- do the find
         
         if the foundChunk is empty then  -- if nothing was found, change tExit to true. Used to exit internal loop. 
            put true into tExit
         else -- else change the color of the text in the found chunk (a chunk being a description of where the text is)
            set the textcolor of the foundchunk to red
         end if
      end repeat
      
   end repeat
end mouseUp

Also, repeat until loops are easy to hang. During testing and development its not a bad idea to give yourself an easy out.

Something like "If the shiftkey is down then exit to top" will work. That way if you have a runaway loop, just hold down shift till it exits. (the code for this has to be inside the loop you want to exit of course.

Rod Edwardson
Posts: 23
Joined: Sun Mar 03, 2013 1:09 am

Re: Find function

Post by Rod Edwardson » Sun Mar 03, 2013 3:55 am

Amazing community here. Thanks so much for the help!

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Find function

Post by sturgis » Sun Mar 03, 2013 1:59 pm

You might want to put a lock screen and unlock screen around your repeat loop also (if you don't care about seeing the find box jumping around in the field) it will make the response time for the search MUCH faster if the screen doesn't need to be updated till completion.

Code: Select all

on mouseUp
   put "dog,horse,pig,geese,goose" into tWords

lock screen -- begin the lock
   repeat for each item tItem in tWords --repeat for each item (comma is default item delimiter)
      -- the repeat places each item into the temporary varaible tItem for use in the script 
      
      find empty -- make sure find is starting fresh
      put false into tExit -- var used to exit internal loop
      
      repeat until tExit -- if tExit is true then the loop will exit. 
         find words tItem in field 1 -- do the find
         
         if the foundChunk is empty then  -- if nothing was found, change tExit to true. Used to exit internal loop. 
            put true into tExit
         else -- else change the color of the text in the found chunk (a chunk being a description of where the text is)
            set the textcolor of the foundchunk to red
         end if
      end repeat
      
   end repeat
unlock screen -- end the lock 
end mouseUp

Rod Edwardson
Posts: 23
Joined: Sun Mar 03, 2013 1:09 am

Re: Find function

Post by Rod Edwardson » Mon Mar 04, 2013 1:23 am

Ah, yes. that sped it up greatly. It is working fabulously. Thanks again!

Post Reply