Difference between revisions of "Foreach"
Jump to navigation
Jump to search
imported>Odessa (entries are 'stringmap' not 'map' artays) |
imported>Odessa (clarified and added external links) |
||
Line 1: | Line 1: | ||
{{Function | {{Function | ||
|origin = NVSE | |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, "key" and "value". | |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 | 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. | ||
|name = Foreach | |name = Foreach | ||
Line 20: | Line 20: | ||
}} | }} | ||
<pre> | <pre> | ||
array_var | array_var SomeArray | ||
array_var | array_var Entry | ||
foreach | foreach Entry <- SomeArray | ||
; | ; 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 | ||
loop | loop | ||
</pre> | </pre> | ||
Line 31: | Line 31: | ||
==Example== | ==Example== | ||
<pre> | <pre> | ||
array_var Beatles | |||
array_var | array_var Entry | ||
array_var | |||
ref rMusician | ref rMusician | ||
int iPosition | int iPosition | ||
let | let Beatles := ar_List JohnREF, PaulREF, GeorgeREF, RingoREF | ||
foreach | foreach Entry <- Beatles | ||
let iPosition := | let iPosition := Entry["key"] | ||
let rMusician := | let rMusician := Entry["value"] | ||
Print "Entry #" + $iPosition + " is " + $rMusician | |||
loop | loop | ||
; Will print in game: | ; Will print in game: | ||
; Entry 0 is John Lennon | ; Entry #0 is John Lennon | ||
; Entry 1 is Paul McCartney | ; Entry #1 is Paul McCartney | ||
; Entry 2 is George Harrison | ; Entry #2 is George Harrison | ||
; Entry 3 is Ringo Starr | ; Entry #3 is Ringo Starr | ||
</pre> | </pre> | ||
Using <b>continue</b>: | |||
<pre> | <pre> | ||
foreach Entry <- Beatles | |||
let rMusician := Entry["value"] | |||
foreach | |||
let rMusician := | |||
if rMusician.GetDead | if rMusician.GetDead | ||
continue ; * Go direct to next entry: we ignore dead members | continue ; * Go direct to next entry: we ignore dead members | ||
endif | endif | ||
rMusician.AddItem Beer 1 | rMusician.AddItem Beer, 1 | ||
loop | loop | ||
; Every living member of the Beatles is given a beer | ; Every living member of the Beatles is given a beer | ||
</pre> | </pre> | ||
Using <b>break</b>: | |||
<pre> | <pre> | ||
foreach Entry <- Beatles | |||
let rMusician := Entry["value"] | |||
foreach | if rMusician.GetInWorldSpace Liverpool | ||
let rMusician := | rMusician.AddItem Beer, 1 | ||
if rMusician.GetInWorldSpace Liverpool | else | ||
break ; * End Loop immediately if we find a member is not in Liverpool | break ; * End Loop immediately if we find a member is not in Liverpool | ||
endif | endif | ||
loop | loop | ||
; Give a beer to each member of the Beatles until one is found not to be in Liverpool- assume that all Beatles except Paul are in Liverpool; since Paul is the second entry of the array, only John, the first, gets a beer. | ; Give a beer to each member of the Beatles until one is found not to be in Liverpool- assume that all Beatles except Paul are in Liverpool; since Paul is the second entry of the array, only John, the first, gets a beer. | ||
</pre> | |||
You can also use the [[Ar_Range]] command to approximate the traditional 'C' style for loop. The code below [[Print|prints]] the numbers 0-10: | |||
<pre> | |||
foreach entry <- (Ar_Range 0, 10) | |||
Print $entry["value"] | |||
loop | |||
</pre> | </pre> | ||
==See Also== | ==See Also== | ||
*[[While]] | |||
*[[Array Variables]] | *[[Array Variables]] | ||
*[[Let]] | *[[Let]] | ||
*[[Eval]] | *[[Eval]] | ||
*[[Label]] | *[[Label]]/[[Goto]] | ||
*[[ | ==External Links== | ||
*[http://obse.silverlock.org/obse_command_doc.html#OBSE_Expressions List of supported expression found in OBSE, these are equivalent in NVSE] | |||
*[http://fallout.bethsoft.com/eng/links/privacyredirect.php?site=http://www.loverslab.com/topic/26749-tutorial-nvse4-part-1-syntax-and-expressions/ Tutorial on syntax and expressions in NVSE 4] | |||
[[Category:Functions_(NVSE)]] | [[Category:Functions_(NVSE)]] | ||
[[Category:Commands]] | [[Category:Commands]] | ||
[[Category:Scripting]] | [[Category:Scripting]] |
Revision as of 08:13, 28 August 2014
< [[::Category:Functions|Category:Functions]]
A function added by the New Vegas Script Extender.
Description
Added by NVSE V4. Used to repeat a script block for each entry in an array. Each entry is itself a stringmap array with two fields, "key" and "value".
Within foreach loops, the command continue may be used to skip any remaining loop code for that entry, and move on to the next. In the same context, the command break may be used to end the loop immediately, ignoring any remaining code and entries.
Syntax
Foreach entry:array <- set:array
array_var SomeArray array_var Entry foreach Entry <- SomeArray ; 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 loop
Example
array_var Beatles array_var Entry ref rMusician int iPosition let Beatles := ar_List JohnREF, PaulREF, GeorgeREF, RingoREF foreach Entry <- Beatles let iPosition := Entry["key"] let rMusician := Entry["value"] Print "Entry #" + $iPosition + " is " + $rMusician loop ; Will print in game: ; Entry #0 is John Lennon ; Entry #1 is Paul McCartney ; Entry #2 is George Harrison ; Entry #3 is Ringo Starr
Using continue:
foreach Entry <- Beatles let rMusician := Entry["value"] if rMusician.GetDead continue ; * Go direct to next entry: we ignore dead members endif rMusician.AddItem Beer, 1 loop ; Every living member of the Beatles is given a beer
Using break:
foreach Entry <- Beatles let rMusician := Entry["value"] if rMusician.GetInWorldSpace Liverpool rMusician.AddItem Beer, 1 else break ; * End Loop immediately if we find a member is not in Liverpool endif loop ; Give a beer to each member of the Beatles until one is found not to be in Liverpool- assume that all Beatles except Paul are in Liverpool; since Paul is the second entry of the array, only John, the first, gets a beer.
You can also use the Ar_Range command to approximate the traditional 'C' style for loop. The code below prints the numbers 0-10:
foreach entry <- (Ar_Range 0, 10) Print $entry["value"] loop