Looping through each line of a field
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
-
- Posts: 35
- Joined: Sun Feb 02, 2014 12:20 am
Looping through each line of a field
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
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
Re: Looping through each line of a field
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
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!
-
- Posts: 35
- Joined: Sun Feb 02, 2014 12:20 am
Re: Looping through each line of a field
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.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
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*
Re: Looping through each line of a field
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...
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
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
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!
-
- Posts: 35
- Joined: Sun Feb 02, 2014 12:20 am
Re: Looping through each line of a field
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 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...That will give you the line number.Code: Select all
repeat for each line myObject in field 1 add 1 to x
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
Re: Looping through each line of a field
only slightly differentI'm actually trying to set the whole line to blue
x is counting the lines.
Code: Select all
on mouseUp
answer line 3 of field "History"
end mouseUp
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!
Re: Looping through each line of a field
Here's an example using HTML in the field. My field is named 'Data'

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
Re: Looping through each line of a field
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".
A slight change with comments:
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.
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
HyperActive Software | http://www.hyperactivesw.com
-
- Posts: 35
- Joined: Sun Feb 02, 2014 12:20 am
Re: Looping through each line of a field
Thanks for the help guys! 
