Difference between revisions of "Foreach"
exapnded, it works for containers + strings too
imported>Odessa (note on direct array access) |
imported>Odessa (exapnded, it works for containers + strings too) |
||
Line 1: | Line 1: | ||
Added by NVSE 4. Used to repeat a script block for each entry in a collection. The collection may be either a container, [[Array Variable|array]] or [[String Variable|string]]. In the case of a container, an entry is a [[Ref Variable|ref]] to an item in its inventory. For an array, each entry is a stringmap with two fields, <b>"key"</b> and <b>"value"</b>. For a string, each entry is a string containing a single character. | |||
Within foreach loops, the command <b>continue</b> may be used to skip any remaining loop code for that entry, and move on to the next. In the same context, the command <b>break</b> may be used to end the loop immediately, ignoring any remaining code and entries. | Within foreach loops, the command <b>continue</b> may be used to skip any remaining loop code for that entry, and move on to the next. In the same context, the command <b>break</b> may be used to end the loop immediately, ignoring any remaining code and entries. | ||
==Syntax== | |||
<pre> | |||
foreach Item:ref <- Container:ref | |||
; Item is an item in container | |||
loop | |||
</pre> | |||
<pre> | <pre> | ||
foreach Entry:array <- Collection:array | |||
foreach Entry <- | |||
; Entry["key"] is the key (index) of each entry (0, 1, 2... for lists) | ; Entry["key"] is the key (index) of each entry (0, 1, 2... for lists) | ||
; Entry["value"] is the value of each entry in the array | ; Entry["value"] is the value of each entry in the array | ||
loop | |||
</pre> | |||
<pre> | |||
foreach Char:string <- String:string | |||
; Char is a single character in String | |||
loop | loop | ||
</pre> | </pre> |