Garry's Mod Wiki

table.foreach

table.foreach( table tbl, function callback )

Description

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.

This is inherited from the original Lua implementation and is deprecated in Lua as of 5.1; see here. You should use pairs instead. The GLua interpretation of this is table.ForEach.

Arguments

1 table tbl
The table to iterate over.
2 function callback
The function to run for each key and value.

Example

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)
Output:
1 Cake 2 Pies Awesome Pizza Delicious Cookies

Example

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)
Output:
1 One 2 Two

Page Links


Special Pages


Wikis

?

Render Time: 34ms

DB GetPage 4
Generate Html 6
SaveChanges (1) 10
Render Body 0
Render Sidebar 11