Iterators
Iteration is the process of stepping through a sequence of values. An iterator is an entity that provides such a sequence of values, one at a time. In SenseTalk, lists, ranges and property lists can all be used as iterators. In addition, you can create your own custom iterator objects. There are several ways that you can step through each of the values supplied by an iterator and work with each one in turn.
Iterating Using Repeat With Each
When you need to work with each value supplied by an iterator and perform some actions with that value, use a repeat with each loop. For example, here's a loop that uses each value from a list:
repeat with each color in ["red", "orange", "yellow", "green"]
put "I think " & color & " is a very pretty color."
end repeat
To iterate over all of the values in a range, use repeat with each just as you would with a list:
repeat with each letter in "A" to "G"
repeat with each digit in 1 to 3
put letter & digit -- Puts "A1", "A2", "A3", "B1", "B2", ...
end repeat
end repeat
Iterating Using Each Expressions
When you need to process each value (or a selected set of values) from a list or other iterator to produce a list of result values, an each expression can be very effective. The result of an each expression is always a list. See Each Expressions.