table.foreach
Lua function

table.foreach

Summary

Applies a function to each item in a table

Prototype

val = table.foreach (t, f)



Description

Executes f for each element in table t.

Function f is called with the arguments (key, value).

If f returns a non-nil value the loop is broken, and this value is returned as the result from table.foreach. Effectively this could be used to find an element inside a table matching a certain condition.


t = { "the", "quick", "brown", "fox", name = "Nick" }
table.foreach (t, print)

 -->
 
1 the
2 quick
3 brown
4 fox
name Nick


Warning the use of table.foreach is deprecated. This means it may not be available in future versions of Lua. You are recommended to rewrite such uses by using the 'pairs' function. For example:


for k, v in pairs (t) do
  f (k, v)
end 



See Also ...

Topics

DOC_lua_base Lua base functions
DOC_lua_bc Lua bc (big number) functions
DOC_lua_bit Lua bit manipulation functions
DOC_lua_coroutines Lua coroutine functions
DOC_lua_debug Lua debug functions
DOC_lua_io Lua io functions
DOC_lua_math Lua math functions
DOC_lua_os Lua os functions
DOC_lua_package Lua package functions
DOC_lua_rex Lua PCRE regular expression functions
DOC_lua Lua script extensions
DOC_lua_string Lua string functions
DOC_lua_tables Lua table functions
DOC_lua_utils Lua utilities
DOC_scripting Scripting

Lua functions

LUA_table.concat table.concat (Concatenates table items together into a string)
LUA_table.foreachi table.foreachi (Applies a function to each item in a numerically-keyed table)
LUA_table.getn table.getn (Returns the size of a numerically-keyed table)
LUA_table.insert table.insert (Inserts a new item into a numerically-keyed table)
LUA_table.maxn table.maxn (Returns the highest numeric key in the table)
LUA_table.remove table.remove (Removes an item from a numerically-keyed table)
LUA_table.setn table.setn (Sets the size of a table (obsolete))
LUA_table.sort table.sort (Sorts a table)

(Help topic: lua=table.foreach)

DOC_contents Documentation contents page