Garry's Mod Wiki

Revision Difference

table.foreach#511808

<function name="foreach" parent="table" type="libraryfunc">⤶ <description>⤶ <deprecated>This was deprecated in Lua 5.1 and removed in 5.2. You should use <page>Global.pairs</page>() instead.</deprecated> 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](http://lua-users.org/wiki/TableLibraryTutorial). You should use <page>Global.pairs</page>() instead. The GLua interpretation of this is <page>table.ForEach</page>.⤶ </description>⤶ <realm>Shared and Menu</realm>⤶ <args>⤶ <arg name="tbl" type="table">The table to iterate over.</arg>⤶ <arg name="callback" type="function">The function to run for each key and value.</arg>⤶ </args>⤶ </function>⤶ ⤶ <example>⤶ <description>Demonstrates the use of this function.</description>⤶ <code>⤶ local food = { "Cake", "Pies", Delicious = "Cookies", Awesome = "Pizza" }⤶ table.foreach( food, function( key, value )⤶ print( tostring(key) .." ".. value)⤶ end)⤶ </code>⤶ <output>⤶ ⤶ ```⤶ 1 Cake⤶ 2 Pies⤶ Awesome Pizza⤶ Delicious Cookies⤶ ```⤶ ⤶ </output>⤶ ⤶ </example>⤶ ⤶ ⤶ <example>⤶ <description>Demonstrates the breaking effect if the callback returns a value.</description>⤶ <code>⤶ local tbl = { "One", "Two", "Three", "Four" }⤶ table.foreach( tbl, function( key, value )⤶ print( key, value )⤶ if key == 2 then return true end⤶ end)⤶ </code>⤶ <output>⤶ ⤶ ```⤶ 1 One⤶ 2 Two⤶ ```⤶ ⤶ </output>⤶ ⤶ </example>