Function; foreach - HWRM/KarosGraveyard GitHub Wiki
foreach(<tTable>, <fFunction>)
(from LUA 4 Manual: 6.1 Basic Functions)
"Executes the given <fFunction> over all elements of <tTable>. For each element, the function is called with the index and respective value as arguments. If the function returns any non-nil value, then the loop is broken, and this value is returned as the final value of foreach. This function could be defined in Lua:"
function foreach (t, f)
for i, v in t do
local res = f(i, v)
if res then return res end
end
end
"The behavior of foreach is undefined if you change the table t during the traversal."
tbl = {
x = 10,
foo = "bar",
hello = "world
};
print("printing: " .. tostring(tbl));
foreach(tbl, function(index, value)
print("\t[" .. tostring(index) .. "]: " .. tostring(value));
end);
print("done!");
<tTable>: the table to traverse.
<fFunction>: the function to execute over all elements of <tTable>. Note: do not add trailing parentheses when entering the function name.
LUA 4 Manual: 6.1 Basic Functions
Updated Formatting? Initial
Updated for HWRM? Initial