Method foreach()
- Method
foreach
voidforeach(arraya,mixedlvalue)- Description
Loop over an array or an Iterator or a value supported by get_iterator().
- Parameter
a Array to loop over in sequence starting at index
0.- Parameter
lvalue Variable or equivalent that will be assigned a value from the array.
There are two syntaxes, The classic array-style syntax:
foreach(({ 1, 2, 3 }), mixed val) { // This block of code will execute 3 times, // with val having the values 1, 2 and 3 in sequence. }Note that the above syntax only supports looping over arrays.
And the Iterator-style syntax:
Iterator|array|mapping|multiset|object|string iter; foreach(iter; mixed ind; mixed val) { // predef::get_iterator(iter) will be called once to create // an iterator if iter is not already an iterator, and then // this code clock will be looped over until // predef::iterator_next() returns false. The lvalues // ind and val will be assigned the values from // predef::iterator_index() and predef::iterator_value() // respectively. Either (or both) of ind and val may // be omitted. }Note the semi-colons used to separate the parameters to foreach above.
- Note
foreach is not a function, but a special form.
- See also
Iterator, get_iterator(), lfun::_get_iterator(), iterator_next(), iterator_index(),
Iterator_value().