table.foreach

From Garry's Mod
Jump to: navigation, search
 table.foreach( )

Contents

Description

This feature is deprecated.
You should avoid using it as it may be removed in a future version.

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

table tbl

The table to iterate over.

Arguments

function callback

The function to run for each key and value.

Examples

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

Examples

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
Personal tools
Navigation