search

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

robm80
Posts: 161
Joined: Sat May 03, 2014 7:15 am

search

Post by robm80 » Sun Aug 24, 2014 8:27 am

How to search for a word and after it is found to mark it in some way.
like underlining or coloring
Part of the script:

Code: Select all

  put number of cards into tNum
      repeat with i=2 to tNum
         if tSearchString is in fld "comment" of cd i then
              put tSearchString &&"on card"&& word 2 of name of cd i& CR&\
              after tResult
         end if
      end repeat

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: search

Post by jmburnod » Sun Aug 24, 2014 9:30 am

Hi Rob,

One way to do that with offset function
Something like that:

Code: Select all

on setStyleFoundWord pFld,pString
   put the length of pString into nbc
   set the textstyle of char 1 to -1 of fld pFld to plain
   put offset(pString,fld pFld) into tOffset
   set the textstyle of char tOffset to (tOffset + nbc-1) of fld pFld to underline
end setStyleFoundWord
Best regards
Jean-Marc
https://alternatic.ch

robm80
Posts: 161
Joined: Sat May 03, 2014 7:15 am

Re: search

Post by robm80 » Sun Aug 24, 2014 10:14 am

Thanks J-M, I'll try.
Rob

robm80
Posts: 161
Joined: Sat May 03, 2014 7:15 am

Re: search

Post by robm80 » Sun Aug 24, 2014 11:01 am

This how your script is implemented, but no Underlinig, but an errormessage:
button "zoek": execution error at line 13 (Handler: can't find handler) near "info", char 38

Code: Select all

on mouseUp tFld,tZoekstring
   if visible of btn "foundbox" is false then
      set visible of btn "foundbox" to true
      ask "put string or lettercombination"
      put it into tZoekstring
      
      if tZoekstring is empty then
         break
      end if
      put number of cards into tNum
      repeat with i=2 to tNum
         if tZoekstring is in fld "comment" of cd i then
            put  fld "comment" of cd i info tFld --<<---------errormessage
            put tZoekstring &&"op card"&& word 2 of name of cd i&\
            CR after tResult
         end if
      end repeat
      delete char -1 of tResult
      sort tResult
      put tResult into btn "foundbox"
   else
      set visible of btn "foundbox" to false
   end if
end mouseUp  

on setStyleFoundWord tFld,tZoekstring
   put the length of tZoekstring into nbc
   set the textstyle of char 1 to -1 of fld tFld to plain
   put offset(tZoekstring,fld tFld) into tOffset
   set the textstyle of char tOffset to&\
   (tOffset + nbc-1) of fld tFld to underline
end setStyleFoundWord
What's wrong?
Rob

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: search

Post by Klaus » Sun Aug 24, 2014 11:45 am

Replace the F with a T! 8)

robm80
Posts: 161
Joined: Sat May 03, 2014 7:15 am

Re: search

Post by robm80 » Sun Aug 24, 2014 2:51 pm

Replace the F with a T
To see it or to see it or to see it not :roll:
Yes, no errormessage anymore, but the script gives no underlining at all :(
More ideas :?:
Thanks, Rob

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: search

Post by Klaus » Sun Aug 24, 2014 3:11 pm

Rob,

you are not calling "setStyleFoundWord" anywhere in your "mouseup" handler, but you should 8)


Best

Klaus

robm80
Posts: 161
Joined: Sat May 03, 2014 7:15 am

Re: search

Post by robm80 » Sun Aug 24, 2014 5:43 pm

Still I get an errormessage.
Stead "on setStyleFoundWord, tFld,tZoekstring, I introduced the Global tFld,tZoekstring
because "on mouseUp tFld,tZoekstring" did nothing.

Code: Select all

on mouseUp
   global tFld,tZoekstring
   put empty into tZoekstring
   if visible of btn "foundbox" is false then
      set visible of btn "foundbox" to true
      ask "Vul zoekstring of lettercombinatie in"
      put it into tZoekstring
      
      if tZoekstring is empty then
         break
      end if
      
      put number of cards into tNum
      repeat with i=2 to tNum
         if tZoekstring is in fld "comment" of cd i then
            put fld "comment" of cd i into tFld
            put tZoekstring &&"op card"&& word 2 of name of cd i &\
            CR after tResult
         end if
      end repeat
      delete char -1 of tResult
      sort tResult
      put tResult into btn "foundbox"
   else
      set visible of btn "foundbox" to false
   end if
   send setStyleFoundWord----------, tFld,tZoekstring (right or wrong ?)
end mouseUp  

on setStyleFoundWord
   global tFld,tZoekstring
   put the length of tZoekstring into nbc
   set the textstyle of char 1 to -1 of tFld to plain   --<<-----errormessage
   put offset(tZoekstring,fld tFld) into tOffset
   set the textstyle of char tOffset to (tOffset + nbc-1) of fld tFld to underline
end setStyleFoundWord
The errormessage contains the whole fld "comment" and is far too long to get copied, but here a part of it:
(Chunk: error in object expression)
Hope you get it!
Rob

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: search

Post by Klaus » Sun Aug 24, 2014 6:39 pm

Hi Rob,

1. instead fo putting the NAME of your field into the global variable tFld
you put the CONTENT of that field into variable:

Code: Select all

...
if tZoekstring is in fld "comment" of cd i then
## put fld "comment" of cd i into tFld
put "comment" into tFld
...
2.Do not use "send" here, this is not neccessary, just call the handler:

Code: Select all

...
 end if
  setStyleFoundWord
  ## tFld,tZoekstring (right or wrong ?)
  ## No, you changed the script to use GLOBAL VARIABLES, didn't you?
end mouseUp
3. you forgot the object descriptor:

Code: Select all

...
## set the textstyle of char 1 to -1 of tFld to plain   --<<-----errormessage
set the textstyle of char 1 to -1 of FLD tFld to plain
...
Hope you get it!
Hope YOU get it! 8)


Best

Klaus

robm80
Posts: 161
Joined: Sat May 03, 2014 7:15 am

Re: search

Post by robm80 » Sun Aug 24, 2014 9:28 pm

No, I don't get it :oops:
I have to say that the second part of the script (by J-M) is academic stuff for me, iow too complicated.
There are no errormessages anymore, but the script does nothing al all: no underlining of the searchstring.
Accidently, by hitting <Enter> the whole text is underlined, totally unexpected.
Rob

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: search

Post by jmburnod » Sun Aug 24, 2014 11:16 pm

hi Rob,
Klaus is definitively right 8) , tfld must contain only the field name
Please try this (no tested):

Code: Select all

on mouseUp
   --jmb mod. Using param instead global variable
   -- global tFld,tZoekstring
   put empty into tZoekstring
   if visible of btn "foundbox" is false then
      set visible of btn "foundbox" to true
      ask "Vul zoekstring of lettercombinatie in"
      put it into tZoekstring
      if tZoekstring is empty then
         break
      end if
      put number of cards into tNum
      repeat with i=2 to tNum
         if tZoekstring is in fld "comment" of cd i then
            --jmb mod
            --   put fld "comment" of cd i into tFld
            put  "comment"  into tFld -- that was the tick as Klaus said
            put tZoekstring &&"op card"&& word 2 of name of cd i &\
            CR after tResult
         end if
      end repeat
      delete char -1 of tResult
      sort tResult
      put tResult into btn "foundbox"
   else
      set visible of btn "foundbox" to false
   end if
   --jmb mod
   --setStyleFoundWord ----------, tFld,tZoekstring (right or wrong ?)
   setStyleFoundWord tFld,tZoekstring 
end mouseUp  

on setStyleFoundWord pFld,pZoekstring
   --jmb mod. Using param instead global variable
   -- global tFld,tZoekstring
   set the textstyle of char 1 to -1 of pFld to plain   --<<-----errormessage. 
   put the length of pZoekstring into nbc
   put offset(tZoekstring,fld pFld) into tOffset
   set the textstyle of char tOffset to (tOffset + nbc-1) of fld pFld to underline
end setStyleFoundWord
Best regards
Jean-Marc
https://alternatic.ch

robm80
Posts: 161
Joined: Sat May 03, 2014 7:15 am

Re: search

Post by robm80 » Mon Aug 25, 2014 2:07 am

Thanks J-M, but there still is an errormessage in the second line of your part:

Code: Select all

on setStyleFoundWord tFld,tZoekstring
   set the textstyle of char 1 to -1 of tFld to plain  --<<-------errormessage
   put the length of tZoekstring into nbc
   put offset(tZoekstring,fld tFld) into tOffset
   set the textstyle of char tOffset to (tOffset + nbc-1) of fld tFld to underline
end setStyleFoundWord
Errormessage: button "zoek": execution error at line n/a (Chunk: error in object expression) near "comment", char 1.
Hope you see why.
Rob

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: search

Post by jacque » Mon Aug 25, 2014 4:01 am

I'd do this:

Code: Select all

on doFind pString
  repeat
    find pString in fld "comment"
    if the result <> "" or the shiftkey is down then exit repeat
    set the backcolor of the foundchunk to "yellow"
    put the short name of this cd && the foundchunk into tRef
    if tRef is among the lines of tList then
      exit repeat
    else
      put tRef & cr after tList
    end if
  end repeat
end doFind
Look at the "find" command in the dictionary, there are many variations. This example finds matches at the beginnings of words in a field named "comments" and sets the backcolor of each match to yellow. I've put in a safety stop if you want to prematurely abort the loop; hold down the shift key to stop it.

The Find command isn't very fast for huge stacks, but for anything under a few hundred cards it's fast enough.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: search

Post by jmburnod » Mon Aug 25, 2014 8:51 am

Rob,
Hope you see why.
Yes. I forgot "fld" before pFld.

Code: Select all

  set the textstyle of char 1 to -1 of fld pFld to plain   --<<-----errormessage. 
Jean-Marc
https://alternatic.ch

robm80
Posts: 161
Joined: Sat May 03, 2014 7:15 am

Re: search

Post by robm80 » Mon Aug 25, 2014 11:34 am

Jacque:
I implemented your script in the mine:

Code: Select all

on mouseUp
   put empty into tZoekstring
   lock screen
   put empty into btn "foundbox"
   show btn "foundbox"
   ask "What word"
   put it into tZoekstring
   
   if tZoekstring is empty then
      break
   end if
   
   doFind tZoekstring
   unlock screen
end mouseUp

on doFind tZoekstring
   repeat
      find word tZoekstring in fld "comment"
      if the result <> "" or the shiftkey is down then exit repeat
      put the short name of this cd  into tRef
      if tRef is among the lines of tList then
         exit repeat
      else
         put tRef & cr after tList
      end if
   end repeat
   put tList into btn "foundbox"
end doFind
1. The drawn box around the word is sufficient
2. This box is created around the word in the fld "comment"of the first card only.
The names of the other cards are correct, but the word is not marked.
3. In the first try the matched words were colored yellow and a box was drawn.
These marks seem to be there forever. How can I get rid of them the next time
I go to that card.
4. Sometimes all flelds "comment" are totally underlined. This is not allowed! I had to correct it manually!

Please get me rid of the marks, even where I had caused an underlining that does not disappear...
Thanks
Rob

Post Reply