Difference between revisions of "Foreach"

155 bytes added ,  13:41, 14 September 2014
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:
{{Function
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.
|origin = NVSE
|summary = Added by NVSE V4. Used to repeat a script block for each entry in an array. Each entry is itself a [[Array Variables|stringmap array]] with two fields, <b>"key"</b> and <b>"value"</b>.


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==
|name = Foreach
<pre>
|returnType = void
foreach Item:ref <- Container:ref
|arguments =  
    ; Item is an item in container
  {{FunctionArgument
loop
  |Name = entry
</pre>
  |Type = array{{FunctionArgument
  |Name = <-
  |Type =
  }}
  }}{{FunctionArgument
  |Name = set
  |Type = array
  }}
}}
<pre>
<pre>
array_var SomeArray
foreach Entry:array <- Collection:array
array_var Entry
 
foreach Entry <- SomeArray
     ; 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>
Anonymous user