ColorizeScript challenge

Dave Cragg dcragg at lacscentre.co.uk
Wed Apr 26 01:20:38 CDT 2006


On 26 Apr 2006, at 00:27, Ray Horsley wrote:

> Hi Richard,
>
> I've found the 'repeat for each' structure, the first one below, to  
> be incredibly faster than the second and third ones below since it  
> doesn't have to count returns or spaces to get to lines and words.   
> For example, in the second repeat loop I believe the second line  
> 'put line cline of s into curline' is going to take a long time to  
> count all those return characters in long scripts, so I believe  
> replacing the 'repeat while...' structure with another 'repeat for  
> each line' structure would speed things up substantially.

I'm not sure that would be the case. The speed benefit of "repeat for  
each"  is that it can let you avoid making repeated chunk references  
(e.g. line x of y) which take progressively longer on each time  
through the loop. But even in the first repeat structure, the script  
has to use a chunk reference (set the textColor of word curword),  
which will get slower and slower.

The only ways I see to avoid those chunk references is to build the  
script anew using "put ... after". For example, it might be possible  
to rebuild the script as html text, then set the htmlText of the  
field in one go. But setting the htmlText has its overheads too, and  
in this case, as almost every word would be tagged, I wouldn't like  
to predict whether it would be faster or not. (Worth a try?)

Cheers
Dave

>>
>> on colorizescript
>>   if tokencolors["if"] is empty then setupcolors
>>   local s
>>   put field "Editor Field" into s
>>   put s into field "Editor Field"
>>   go to card 2
>>   local curword
>>   put 1 into curword
>>   repeat for each word w in s
>>     if tokencolors[w] is not empty
>>     then set the textColor of word curword of field "Editor Field"  
>> of card 1 to tokencolors[w]
>>     add 1 to curword
>>   end repeat
>>   local oldline, cline, curline, nwords
>>   put min(lineoffset("#", s), lineoffset("--", s)) into oldline
>>   put 0 into cline
>>   repeat while oldline is not 0
>>     add oldline to cline
>>     put line cline of s into curline
>>     put the number of words in curline into nwords
>>     repeat with curword = 1 to nwords
>>       if char 1 of word curword of curline is "#" or char 1 to 2  
>> of word curword of curline is "--" then
>>         set the textColor of word curword to nwords of line cline  
>> of field "Editor Field" of card 1 to "DarkOrchid4"
>>         exit repeat
>>       end if
>>     end repeat
>>     put min(lineoffset("#", s, cline), lineoffset("--", s, cline))  
>> into oldline
>>   end repeat
>>   go to card 1
>>   unlock screen
>> end colorizescript
>>


More information about the metacard mailing list