setting the textstyle of words found after looping

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
glenn9
Posts: 223
Joined: Wed Jan 15, 2020 10:45 pm
Location: Europe

setting the textstyle of words found after looping

Post by glenn9 » Sat Nov 28, 2020 8:56 am

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

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: setting the textstyle of words found after looping

Post by Klaus » Sat Nov 28, 2020 10:29 am

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

glenn9
Posts: 223
Joined: Wed Jan 15, 2020 10:45 pm
Location: Europe

Re: setting the textstyle of words found after looping

Post by glenn9 » Sat Nov 28, 2020 10:50 am

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

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: setting the textstyle of words found after looping

Post by Klaus » Sat Nov 28, 2020 11:08 am

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

glenn9
Posts: 223
Joined: Wed Jan 15, 2020 10:45 pm
Location: Europe

Re: setting the textstyle of words found after looping

Post by glenn9 » Sat Nov 28, 2020 11:45 am

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

jiml
Posts: 336
Joined: Sat Dec 09, 2006 1:27 am
Location: Los Angeles

Re: setting the textstyle of words found after looping

Post by jiml » 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.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9837
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: setting the textstyle of words found after looping

Post by FourthWorld » Sun Nov 29, 2020 3:17 am

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?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”