Can someone explain to me what next() is?
I see it in some scripts but I don't know what the hell it actually means.
Can someone explain to me what next() is?
I see it in some scripts but I don't know what the hell it actually means.
the next function, basically return the next object in a table according on the lastKey.
But, what is "lastKey" ?
its basically the second argument of the next function, if not defined it will return the first element from a table, for example:
local tbl = {'Hello', 'Second'}
print(next(tbl)) -- Hello
print(next(tbl, 1)) -- Second
print(next(tbl, 2)) -- nil (because there is nothing after the index "2")
you can use it to check if has a object in a table, or to do generic for loops, another example:
local tbl = {
'Hello!',
'This is a example of the "next" function!',
':)'
}
for i, v in next, tbl do
print(i, v)
end
--[[
Output:
1 Hello!
2 This is a example of the "next" function!
3 :)
]]
Hope it helped! :)
This discussion is preserved as it was. New answers, comments, and votes are closed.
1 comment
thanks