table.foreach
From GMod Wiki
Function | |
Syntax |
table.foreach( Table t, Function f ) Where is this used? |
Description: | |
Iterate over each pair in t, calling f with the key and value of the pair. | |
Returns: | nil |
Part of Library: | Table |
Realm: | |
BBCode Link: | [b][url=http://wiki.garrysmod.com/?title=Table.foreach]Table.foreach [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] |
Example
Description | Prints the keys and values of the table. |
---|---|
Used on | |
Code | someTable = {a = 1, b = 2, c = 3} function someFunction(k, v) print(k, v) end table.foreach(someTable, someFunction) |
Output | a 1 b 2 |
Additional Notes
- This function is deprecated in Lua 5, in favor of using:
for k, v in pairs(someTable) do someFunction(k, v) end
- This function will iterate over both numeric and non-numeric keys.
- The order of iteration over non-numeric keys in undefined. You are not guaranteed to receive the pairs in the order you defined them in your table.
See Also
- table.foreachi
- Lua Reference Manual (section 5.4)