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.
Iterating Using NextValue
The nextValue function will return the next sequential value from an iterator. When the sequence is exhausted and no more values are available from that iterator, the special constant value end is returned. The currentValue function will return the current value again without advancing the iteration.
put 100 to 200 by 50 into range
put range's nextValue --> 100
put range's nextValue --> 150
put range's currentValue --> 150
put range's nextValue --> 200
put range's nextValue --> ⓔ ⓝ ⓓ