Loops

for/foreach collect


foreach ... collect
looping-statement


The foreach statements have a collect version as well as the standard do version. The collect version, however, has a much different statement value. Each time through the loop, the value of the looping statement is collected into an array. The value of the foreach loop is then that array. The first array entry is the value of the looping statement the first time the loop executes, the second array entry is the value the second time through the loop, and so on. For example:

x := [1, 2, 3, 5];
y := foreach value in x collect
      value * 2;

Print(y);
[2, 4, 6, 10]

An online version of Programming for the Newton using Macintosh, 2nd ed. ©1996, 1994, Julie McKeehan and Neil Rhodes.

Last modified: 1 DEC 1996