Page 1 of 1

Finding if a word is in a list of other words

Posted: Tue Oct 13, 2009 5:14 pm
by gyroscope
I have made a small test for placing into my stack. What I'm after is very straightforward, but I can't work it out (must be the weather):

I've fields "B" and "F" and a button. Field "B" is blank, field "F" has a list of words, one per line. When I type a word into field "B" and press the button, I want to find out if the word typed is included in the list.

Just so you don't think I haven't tried :wink: here are my failed attempts:

Code: Select all

on mouseUp
   put field "B" into tWord
   put field "F" into tAllWords
   if (tWord is among the words of field "F") is true then ---- or is among tAllWords
      answer "Yes, it is!!"
   else
      answer "no, sorry..."
   end if
end mouseUp
   
   on mouseUp
      put field "B" into tWord
      put field "F" into tAllWords
      if tWord is in tAllWords then
         answer "Yes, it is!!"
      else
         answer "no, sorry..."
         end if
      end mouseUp

on mouseUp
        put field "B" into tWord
      put field "F" into tAllWords
      matchText tAllWords(quote &tWord&quote)
      if it is true then
         answer "Yup" 
      else
         answer "Nope!"
         end if
   end mouseUp
   
   on mouseUp
    put field "B" into tWord
   put field "F" into tAllWords
   if (tAllWords contains tWord) is true then
      answer "Yes, it is!!"
   else
      answer "no, sorry..."
   end if
   end mouseUp
I guess I'm going to kick myself when I read the answer...any help appreciated please!

:)

Posted: Tue Oct 13, 2009 7:34 pm
by bn
Gyroscope,

all your versions work for me except the matchtext (wrong syntax), it works if you put

Code: Select all

  get matchText (tAllWords,tWord) 
All of them dont work if you have a space after the word in field "B". So I wonder what is going on? You might change your code to "word 1 of field "B"" or something else is happening. I suspect so since you say none of the scripts work for you, but I dont have an idea.

If you look in the new RevOnline Devin Asay has a stack "type with filter", that narrows from a list of words down to the matches while you type. If that is what you want you might want to look at it.

regards
Bernd

Posted: Tue Oct 13, 2009 9:37 pm
by gyroscope
Hi Bernd
all your versions work for me except the matchtext (wrong syntax)
Well, that is the strangest thing: all I was getting was "No" for the 3 of them, with a matched or unmatched word. But now trying again, including the amended matchText one as per your suggestion, they all work now...very odd.

Except to say, the first one works perfectly, but the other 3 have the same peculiarity: for instance, say one of the words in the list is "dogs", the answer will be "Yes" by inputting "d", "do" or "dog".

So I'll stick with the first one, I think. I can't, for the life of me, work out what was going wrong the first time round to get the wrong results...

But it's working fine now and that's what counts!

Thank you for your advice and matchText script, Bernd.

:)

PS I'll take a look at Devin Asay's stack using filter; that looks to be useful for other things perhaps.

Posted: Wed Oct 14, 2009 9:27 am
by Klaus
Hi gyroscope,

I would probably use "wordoffset" with "dwholematches" set to true:

Code: Select all

...
set the wholematches to true
if wordoffset(word2look4,list_of_other_words) = 0 then
   ## word not in field/list
else
   ## word in list/field
end if
...
Best

Klaus

Posted: Wed Oct 14, 2009 12:35 pm
by gyroscope
That's neat Klaus, thank you!

I'm sure you won't mind me pointing out that the second line of your example should be:

Code: Select all

if wordoffset(word2look4,list_of_other_words) > 0 then
(Perhaps you were testing me :wink:)

:)

Posted: Wed Oct 14, 2009 1:56 pm
by Klaus
gyroscope wrote:...
Perhaps you were testing me :wink:
Yes, and you obviously failed :D

Please note my "if...then...else..." structure! 8)


Best

Klaus

Posted: Wed Oct 14, 2009 2:44 pm
by gyroscope
Ah, flippers, you swapped the "then"s" based on my script...and I can't blame my spectacles every time :wink:

"1 out of 10, must try harder" !! :lol:

:)

Posted: Thu Oct 15, 2009 4:21 pm
by Klaus
:D

Posted: Sat Oct 17, 2009 4:24 pm
by FourthWorld
If you want to see if a word is among the words of a list, you can use:

Code: Select all

if tWord is among the words of tList
If tList contains whole sentences, you might want to run through a few replace commands to strip out periods and the like:

Code: Select all

put fld 1 into tList
put ",.;:[]'"&quote into tStopChars
repeat for each char tChar in tStopChars
   replace tChar with space in tList
end repeat

Posted: Sat Oct 17, 2009 11:01 pm
by gyroscope
Hi Richard

That's excellent; as it happens, I need to temporarily strip punctuation as my major project needs to parse complete sentences. So this is going to be most useful, thank you!

:)