Repeat for each line?

Jeanne A. E. DeVoto jeanne at runrev.com
Thu Apr 18 05:25:01 EDT 2002


At 1:06 AM -0700 4/18/2002, Magnus von Brömsen wrote:
>on addOrder
>   repeat for each line x in field "articles"
>     if the foregroundColor of x = 204,0,0 then

The problem here is that when you use this form of the repeat loop, your
variable "x" contains the actual text of the line. And a variable has no
foregroundColor as such - it's just the line contents - so no go. It's like
saying "if the foregroundColor of "foo" is 204,0,0".

Here's one alternative:

  on addOrder
    repeat with x = 1 to the number of lines of field "articles"
    -- with this form, "x" is the line number, not the contents
      if the foregroundColor of line x of field "articles" is "204,0,0"
      then put line x of field "articles" & return after redLine
    end repeat
    put redLine into field "order"
  end addOrder

That is pretty straightforward - you're checking every line in the field,
in order - but it is not very efficient. If the field "articles" has a lot
of lines, more speed may be needed. Here's another way that should be
faster:

  on addOrder
    repeat for each line thisLine in the htmlText of field "articles"
    -- "thisLine" contains the htmlText, and the htmlText contains
    -- tags for all the formatting, so you can check it for color:
      if "font color="#CC0000" is in thisLine
      then put thisLine & return after redLine
    end repeat
    set the htmlText of field "order" to redLine
    -- include the next line if you want to remove formatting from
    -- the "order" field (formatting is still there because you used
    -- the htmlText):
    put field "order" into field "order"
  end addOrder

--
Jeanne A. E. DeVoto ~ jeanne at runrev.com
Runtime Revolution Limited - The Solution for Software Development
http://www.runrev.com/





More information about the use-livecode mailing list