WoW:API foreach: Difference between revisions
({{luaapi}}) |
m (Move page script moved page API foreach to WoW:API foreach without leaving a redirect) |
||
| (One intermediate revision by one other user not shown) | |||
| Line 1: | Line 1: | ||
{{luaapi}} | {{luaapi}} | ||
Runs the supplied function once for each table element. Is a Lua table module function. (Deprecated in Lua 5.1, use pairs or ipairs instead.) | |||
table.foreach( | local result = table.foreach(t, func) | ||
foreach( | local result = foreach(t, func) | ||
From [ | == Parameters == | ||
=== Arguments === | |||
* t (table) - the table to iterate over | |||
* func (function) - the Lua function to call for each table element. Function arguments are func(index, value). IF function returns non-nil value the integration ends | |||
=== Returns === | |||
* result (any) - If function 'func' returns a non-nil value then this value is returned, else returns 'nil'. | |||
== Summary == | |||
Lua function in the in the Lua 'table' module that automatically iterates over a table, running the supplied Lua function once for each table element. Will apply the function 'func' to the elements of the table passed. On each iteration the function is passed the key-value pair of that element in the table. | |||
== Examples == | |||
From [//lua-users.org/wiki/TableLibraryTutorial lua-users.org/wiki/TableLibraryTutorial]: | |||
> table.foreach({1,"two",3}, print) -- print the key-value pairs | > table.foreach({1,"two",3}, print) -- print the key-value pairs | ||
| Line 31: | Line 43: | ||
pi 3.14159 | pi 3.14159 | ||
banana yellow | banana yellow | ||
== See also == | |||
* table.foreach on [//www.lua.org/manual/5.0/manual.html lua.org/manual/5.0/manual.html] | |||
Latest revision as of 04:45, 15 August 2023
← WoW Lua
Runs the supplied function once for each table element. Is a Lua table module function. (Deprecated in Lua 5.1, use pairs or ipairs instead.)
local result = table.foreach(t, func) local result = foreach(t, func)
Parameters
Arguments
- t (table) - the table to iterate over
- func (function) - the Lua function to call for each table element. Function arguments are func(index, value). IF function returns non-nil value the integration ends
Returns
- result (any) - If function 'func' returns a non-nil value then this value is returned, else returns 'nil'.
Summary
Lua function in the in the Lua 'table' module that automatically iterates over a table, running the supplied Lua function once for each table element. Will apply the function 'func' to the elements of the table passed. On each iteration the function is passed the key-value pair of that element in the table.
Examples
From lua-users.org/wiki/TableLibraryTutorial:
> table.foreach({1,"two",3}, print) -- print the key-value pairs
1 1
2 two
3 3
> table.foreach({1,"two",3,"four"}, function(k,v) print(string.rep(v,k)) end)
1
twotwo
333
fourfourfourfour
If the function f returns a non-nil value the iteration loop terminates.
> table.foreach({1,"two",3}, function(k,v) print(k,v) return k<2 and nil end)
1 1
2 two
Tables can contain mixed key-value and index-value elements. table.foreach() will display all of the elements in a table. To only display the index-value elements see table.foreachi(). For more information about this subject see the TablesTutorial.
> t = { 1,2,"three"; pi=3.14159, banana="yellow" }
> table.foreach(t, print)
1 1
2 2
3 three
pi 3.14159
banana yellow
See also
- table.foreach on lua.org/manual/5.0/manual.html