Examples of Gosu conditional iteration and looping
The following examples illustrate different techniques for iterating through the members of an array or list.
Example: Printing all letters in a string with their index
for (letter in gw.api.util.StringUtil.splitWhitespace("a b c d e") index i) {
print("Letter " + i + ": " + letter)
}
The output of this code is the following
lines:
Letter 0: a Letter 1: b Letter 2: c Letter 3: d Letter 4: e
Example: Printing all Policy properties by using reflection
for (prop in PolicyGosu.Type.TypeInfo.Properties) {
print(prop)
}
The output of this code is the following
lines:
Class Bundle OriginalVersion IntrinsicType Changed ChangedFields DisplayName ChangedProperties NewlyImported ID PublicID ...
Example: Ensuring that array elements have a minimum value
var colors = {"red", "orange", "yellow", "green", "blue", "indigo", "violet"}
var blueshift = new String()
var r : int = (Math.random() * 6) as int
blueshift = colors[r]
// Shift the color at least one step to the blue end of the spectrum
do {
r += 1
blueshift = colors[r]
} while (r < 3) // Minimum color is "green"
print(blueshift)
Running this code multiple times produces lines similar to the
following:
blue green green green green blue violet green blue indigo
