Traverses all entries in a table. Returns the next index, value pair. If index is nil (the default), returns the first pair. When called with the last index of the table (or with an index of nil for an empty table), returns nil.
Only non-nil values are returned. Order is not specified (eg. is not necessarily alphabetic sequence). Behaviour is undefined if you assign values to non-existent fields during the traversal. You may however modify or delete existing fields.
t = { "hello", "world" }
for k, v in next, t do
print (k, v)
end
-->
1 hello
2 world
See also the pairs function, which uses next. The above code could be written as:
for k, v in pairs (t) do
print (k, v)
end
If you want to know if a table is empty, or not, you can test:
if next (t) == nil then
-- table t is empty
end -- if empty