Garry's Mod Wiki


  table.foreach( table tbl, function callback )


We advise against using this. It may be changed or removed in a future update. This was deprecated in Lua 5.1 and removed in 5.2. You should use pairs instead.

Iterates for each key-value pair in the table, calling the function with the key and value of the pair. If the function returns anything, the loop is broken.

The GLua interpretation of this is table.ForEach.


1 table tbl
The table to iterate over.
2 function callback
The function to run for each key and value.
Function argument(s):
1 any key - The key of a key-value pair for this iteration.
2 any val - The value of a key-value pair for this iteration.


Demonstrates the use of this function.

local food = { "Cake", "Pies", Delicious = "Cookies", Awesome = "Pizza" } table.foreach( food, function( key, value ) print( tostring(key) .. " " .. value) end)
1 Cake 2 Pies Awesome Pizza Delicious Cookies


Demonstrates the breaking effect if the callback returns a value.

local tbl = { "One", "Two", "Three", "Four" } table.foreach( tbl, function( key, value ) print( key, value ) if key == 2 then return true end end)
1 One 2 Two