XJTAG 4.1.3 introduces FOREACH loops to iterate over all elements of an array in XJEase.
It is now possible to iterate over all elements of an array, using a clear and concise syntax which may be preferrable to using FOR loops:
GLOBAL ForeachExample1()() INT[] array := {0, 1, 1, 2, 3, 5}; FOREACH INT element IN array PRINT(element, ", "); END; END;
Output:
0, 1, 1, 2, 3, 5,
All data types are supported (INT, STRING, FILE). Following is an example using a STRING array:
GLOBAL ForeachExample2()() STRING[] array := {"2", "3", "5", "7", "11", "13"}; FOREACH STRING element IN array PRINT(element, ", "); END; END;
Output
2, 3, 5, 7, 11, 13,
Arrays of any depth can be used:
GLOBAL ForeachExample3()() INT[][] array2d := {{0, 1, 1, 2, 3}, {2, 3, 5, 7}, {1, 2, 4, 8, 16, 32}}; FOREACH INT[] array IN array2d FOREACH INT element IN array PRINT(element, ", "); END; PRINT("\n"); END; END;
Output:
0, 1, 1, 2, 3, 2, 3, 5, 7, 1, 2, 4, 8, 16, 32,
Changes made to an iterator element will be reflected in the array:
GLOBAL ForeachExample4()() INT[] array := {0, 1, 42}; FOREACH INT element IN array element := element + 1; END; FOREACH INT element IN array PRINT(element, ", "); END; END;
Output:
1, 2, 43,
Leave A Comment