Looping through each line of a field

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
ferhan24badshah
Posts: 35
Joined: Sun Feb 02, 2014 12:20 am

Looping through each line of a field

Post by ferhan24badshah » Thu Mar 13, 2014 8:57 am

I want to loop through each line of a field, and set the textColor of each line to either blue or red depending on the content of the text of each line.

My code is embedded into a button and is as follows:
on mouseUp
repeat for each line myObject in field "History"
if the second word of myObject = "didn't" then
set the textColor of myObject to blue
else if the second word of myObject = "did" then
set the textColor of myObject to red
end if
end repeat
end mouseUp

When I try to run the code above by clicking the button in which it is embedded in, I get an error at the code line "set the textColor of myObject to.."

Do I need to make an adjustment to my code? Thank you

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Looping through each line of a field

Post by Simon » Thu Mar 13, 2014 9:12 am

Hi Ferhan,
Do you see that "myObject" is just a line, it is a line in field "History" but you
set the textColor of myObject to blue
Nothing to do with the field "History"

Can you figure it out from there?

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

ferhan24badshah
Posts: 35
Joined: Sun Feb 02, 2014 12:20 am

Re: Looping through each line of a field

Post by ferhan24badshah » Thu Mar 13, 2014 9:26 am

Simon wrote:Hi Ferhan,
Do you see that "myObject" is just a line, it is a line in field "History" but you
set the textColor of myObject to blue
Nothing to do with the field "History"

Can you figure it out from there?

Simon
Thanks for replying, hmmm......I made an addition to that line code earlier which looked like "set the textColor of myObject in field "History" to blue"...but I was getting a different error *set: missing 'to'* which confused me since the 'to' is not missing in that line code.

But then I modified that line as "set the textColor of myObject to blue in field "History" " which gave me the save error as before *Chunk:error in object expression*

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Looping through each line of a field

Post by Simon » Thu Mar 13, 2014 9:42 am

Ok it is a little complex.
You want to set the second word of a line in fld "History" to blue if it is "didn't"
So you have to figure out what line it is...

Code: Select all

   repeat for each line myObject in field 1
      add 1 to x
That will give you the line number.
Your if/then is good.
Now, set the textColor of (which (the) word of which line of which field) to blue

how about that?

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

ferhan24badshah
Posts: 35
Joined: Sun Feb 02, 2014 12:20 am

Re: Looping through each line of a field

Post by ferhan24badshah » Thu Mar 13, 2014 9:51 am

Simon wrote:Ok it is a little complex.
You want to set the second word of a line in fld "History" to blue if it is "didn't"
So you have to figure out what line it is...

Code: Select all

   repeat for each line myObject in field 1
      add 1 to x
That will give you the line number.
Your if/then is good.
Now, set the textColor of (which (the) word of which line of which field) to blue

how about that?

Simon
Hmm, "add 1 to x" confuses me.....and I'm actually trying to set the whole line to blue which has "didn't" as its second word.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Looping through each line of a field

Post by Simon » Thu Mar 13, 2014 9:57 am

I'm actually trying to set the whole line to blue
only slightly different

x is counting the lines.

Code: Select all

on mouseUp
answer line 3 of field "History"
end mouseUp
See how that works?
Again your if/then is correct, just which line will be red and which will be blue?

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

splash21
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 369
Joined: Sun Dec 19, 2010 1:10 am
Contact:

Re: Looping through each line of a field

Post by splash21 » Thu Mar 13, 2014 10:46 am

Here's an example using HTML in the field. My field is named 'Data' :D

Code: Select all

on mouseUp
   local tData, tLines, tLine, tWord
   
   # grab the contents of the field
   put the text of field "Data" into tData
   
   # count the lines
   put the number of lines in tData into tLines
   
   # process each line
   repeat with tIndex = 1 to tLines
      
      # get the current line
      put line tIndex of tData into tLine
      
      # get word 2 of the current line
      put word 2 of tLine into tWord
      
      # check word 2
      if tWord = "did" then
         # use HTML to make line red + add a line break
         put "<font color='red'>" & tLine & "</font><br>" into tLine
      else if tWord = "didn't" then
         # use HTML to make line blue + add a line break
         put "<font color='blue'>" & tLine & "</font><br>" into tLine
      else
         # add a line break
         put "<br>" after tLine
      end if
      
      # update the line with the modified data
      # since we are using HTML, the lines should all have a linebreak '<br>'
      put tLine into line tIndex of tData
      
   end repeat
   
   # set the HTML of the field to our new data
   set the htmlText of field "Data" to tData
end mouseUp
LiveCode Development & Training : http://splash21.com

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

Re: Looping through each line of a field

Post by jacque » Thu Mar 13, 2014 5:16 pm

When you "repeat for each line", the variable will contain the text of the line. You can't set a color on a variable, you can only do that for a direct reference to a chunk of text in a field. What you want is "repeat with x = 1 to the number of lines in fld y". In that case, the variable "x" will be an integer. Then you can set the color of "line x of fld y".

Code: Select all

repeat for each line myObject in field "History" -- myObject will contain something like "this is some text"
  if the second word of myObject = "didn't" then -- correct, you can test for the content of the text in the variable
    set the textColor of myObject to blue  -- here you are trying to set the color of the variable content "this is some text". You can't.
A slight change with comments:

Code: Select all

repeat with x = 1 to the number of lines in field "History" -- the variable will be an integer that increases by 1 for each iteration
  if the second word of x = "didn't" then -- this will now fail, use "if word 2 of line x of fld "history ="
    set the textColor of x to blue -- use a reference to the field: set the textcolor of word 2 of line x of fld "history"
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

ferhan24badshah
Posts: 35
Joined: Sun Feb 02, 2014 12:20 am

Re: Looping through each line of a field

Post by ferhan24badshah » Thu Mar 13, 2014 6:25 pm

Thanks for the help guys! 8)

Post Reply