table.foreach

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 

Lua functions

Topics