table.foreach

Calls func once for each key-value pair in the tbl. Specifically,

<nowiki>
table.foreach(tbl, func)
</nowiki>

table.foreach
Function
Syntax
table.foreach(
  • tbl : table
  • func : function
)

Returns nil
API table
Source Lua (source)
This function is deprecated.[1]

is equivalent to

<nowiki>
for key, value in pairs(tbl) do
  if func(key, value) ~= nil then
    break
  end
end
</nowiki>
ExamplePrint the elements of a table
Print out each key-value pair in tbl.
Code
<nowiki>
local tbl = { a = 1, b = 2, 'foo', 'bar' }
table.foreach(tbl, function(k, v)
  print(k, v)
end)
    </nowiki>
Output
<nowiki>
a 1
b 2
1 foo
2 bar
</nowiki>

References

This article is issued from Computercraft. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.