Ar ForEach

From GECK
Jump to: navigation, search


A function added by the New Vegas Script Extender version 6.2.1.

Description

Takes an array and a function and executes the function once for each array element.

Syntax

[help]
Ar_ForEach source:array function:function/lambda 

Example

array_var aIter
array_var aWords = ar_List "spray", "limit", "elite", "exuberant", "destruction", "present"

ar_ForEach aWords ({aIter} => print *aIter);
; prints all items in the list
array = ar_list 4 9 100 1 43

; get the lowest value of an array
lowest = ar_first array
ar_ForEach array ({aIter} => lowest = GetMinOf (*aIter) lowest)
; lowest is now 1
; multiple lines (see Lambda wiki page)
ar_ForEach array (begin function {array_var aIter}
  print "the value of aIter is on the next line"
  print $(*aIter)
end)

Notes

  • To see why the * operator is used and why aIter is an array_var, see Dereference/Unbox in NVSE Expressions.
  • aIter["key"] will contain the index of aWords. If aWords was a String Map then it would contain the string key.
  • This is an alternative to ForEach which may be less verbose, but is less flexible (can't use control-flow statements like Break).

See Also