Repeat for each loop assistance, please.

Rob Cozens rcozens at pon.net
Sun Aug 3 00:57:32 EDT 2003


Hi Andy,

>What can I query to get the iteration number of this loop?
>

There is no syntax to retrieve the iteration number in a repeat for each loop;
so one must initialize and increment a counter variable:

put 0 into lineCount
repeat for each line gameLine in fld "gamelist"
	add 1 to lineCount
	if errorFound(gameLine) then
		answer "Error found!"
		select line lineCount of field "gameList"
		exit to top
	end if
end repeat

>Perhaps I'd be better off using a different repeat construct?

If speed is important, you will not obtain anywhere near the performance with

repeat with x = 1 to the number of lines of field "gamelist"
	put line x of field "gamelist" into gameLine
	if errorFound(gameLine) then
		answer "Error found!"
		select line x of field "gameList"
		exit to top
	end if
end repeat

or even

get field "gamelist"
repeat with x = 1 to the number of lines of it
	put line x of it into gameLine
	if errorFound(gameLine) then
		answer "Error found!"
		select line x of field "gameList"
		exit to top
	end if
end repeat

Repeat for each can easily be an order of magnitude faster than 
repeat with...the larger the field, the greater the relative 
efficiency.
-- 

Rob Cozens
CCW, Serendipity Software Company
http://www.oenolog.com/who.htm

"And I, which was two fooles, do so grow three;
Who are a little wise, the best fooles bee."

from "The Triple Foole" by John Donne (1572-1631)



More information about the use-livecode mailing list