foreach

Syntax

foreach variable in [list] {BLOCK}

foreach index, variable in [list] {BLOCK}

foreach index, variable in {list} {BLOCK}

foreach index in {list} {BLOCK}

foreach index, variable in list {BLOCK}

foreach variable in range {BLOCK}

Description

The foreach loop iterates over list and sets variable to be each element of list, performing BLOCK for each element of list in turn.

Example

// Iterating over the contents of an array
println( "Array: value\n")
a = [ "My", "name", "is", "Neddie" ]
foreach item in a {
   	println( "\t",  item);
}
println( "Array: index/value\n")
foreach index, item in a {
    	println( "\t", index, "\t",  item );
}

// Iterating over the contents of a string.
println( "String: value\n")
foreach char in "Neddie Seagoon" {
     	println( "\t", char );
}

// character + index
println( "String: index/value\n")
foreach idx, char in "Neddie Seagoon" {
  	println( "\t", idx, "\t", char );
}

// Counting through a range
foreach num in 1..10 {
    println(num)
}