Page 1 of 1

setting the textstyle of words found after looping

Posted: Sat Nov 28, 2020 8:56 am
by glenn9
Hi everyone,

Grateful for any hints or tips on how to solve this problem I've encountered...

I have two cards, Card 1 has field A containing a list of strings and Card 2 has a field B containing a paragraph of text.

I'm trying to check if a word in field B is the same as one of the lines of field A.

the code below achieves this:

Code: Select all

 repeat for each word x in field"B" of card"2"
      repeat for each line y in field"A" of card"1" 
         if x is y then
         answer x
          
          
but I'd then like to underline each word x. I thought that adding this code to above

Code: Select all

  set the textstyle of x to underline
would achieve this... but it doesn't!

I've trialed and errored many permutations but not sure why this doesn't work!

Grateful for any hints.

Thanks,

Glenn

Re: setting the textstyle of words found after looping

Posted: Sat Nov 28, 2020 10:29 am
by Klaus
Hi glenn,

x only contains the WORD e.g. "Yiiihaw" but not its position in the field, so this does not work.
Try something like this:

Code: Select all

...
## repeat for each is extremely fast, but in this case we need to manage our own counter:
   put 1 into tWordCounter

   ## Loop throught words:
   repeat for each word x in field "B" of card "2"
      
      ## Loop through lines
      repeat for each line y in field"A" of card"1" 
         if x = y then
            set the textstyle of WORD tCounterx of fld "B" of cd 2 to underline
            ## We should only set the textstyle ONCE so in this case:
            exit repeat
         end if
      end repeat
      add 1 to tWordCounter
   end repeat
...
IMPORTANT:
-> of card "2"
Did you really name your card with a number? If yes, DON'T!
Since the engine is "typeless", this will be interpreted by the engine as just -> card 2

Best

Klaus

Re: setting the textstyle of words found after looping

Posted: Sat Nov 28, 2020 10:50 am
by glenn9
Hi Klaus,

Many thanks, noted re naming cards etc

Have just tried your code but I was confused by this line

Code: Select all

...set the textstyle of WORD tCounterx...
as wasn't sure where the tCounterx came from...

Have tried it with just x and also tWordCounter but no luck so far...

Not sure what I'm not understanding!

Regards,

Glenn

Re: setting the textstyle of words found after looping

Posted: Sat Nov 28, 2020 11:08 am
by Klaus
Hi Glenn,

sorry, that was a remnant of copy/paste! :oops:

tWordCounter is correct, just made a little test and works as exspected:

Code: Select all

on mouseUp pMouseButton
   put 1 into tWordCounter
   repeat for each word x in field 1
      
      repeat for each line y in field 2
         if x = y then
            set the textstyle of WORD tWordCounter of fld 1 to "underline"
            ## We should oly set the textstyle ONCE so in this case:
            exit repeat
         end if
      end repeat
      add 1 to tWordCounter
   end repeat   
end mouseUp
Just replace fld 1 and fld 2 with your field references and that should be it.

Best

Klaus

Re: setting the textstyle of words found after looping

Posted: Sat Nov 28, 2020 11:45 am
by glenn9
Hi Klaus,

Thank you so much, all works, and I think I now understand why it wasn't working before - I was again not appreciating that the 'for each' returns a string but I need the word number so that the textstyle to be applied.

Kind regards,

Glenn

Re: setting the textstyle of words found after looping

Posted: Sun Nov 29, 2020 12:05 am
by jiml
And just for fun another way:

Code: Select all

on mouseUp 
   set the textstyle of char 1 to -1 of fld b to plain
   repeat for each line cLine in fld A
      repeat forever
         find whole cLine in fld b
         if the foundchunk = "" then exit repeat
         do "set the textstyle of" && the foundchunk && "to underline"
      end repeat
   end repeat
end mouseUp
LiveCode usually allows multiple ways to achieve a task.

Re: setting the textstyle of words found after looping

Posted: Sun Nov 29, 2020 3:17 am
by FourthWorld
jiml wrote:
Sun Nov 29, 2020 12:05 am
And just for fun another way:

Code: Select all

on mouseUp 
   set the textstyle of char 1 to -1 of fld b to plain
   repeat for each line cLine in fld A
      repeat forever
         find whole cLine in fld b
         if the foundchunk = "" then exit repeat
         do "set the textstyle of" && the foundchunk && "to underline"
      end repeat
   end repeat
end mouseUp
LiveCode usually allows multiple ways to achieve a task.
Is "do" needed there? That should evaluate properly without that, no?