Question: What is next?

Hi,

So I was wondering what is next?

Looking at its Description, and Documentation, its used in for loops

for i,p in next() do -- What is this? Am i using it Correctly?
	
end

Is it used like ipairs or pairs?

Next returns the next index and value in a table given the table and its previous value

i1, first = next(t, nil)
i2, second = next(t, first)

This forms the basis of a “stateless” iterator, it is stateless because you don’t have a loop variable keeping track of the current iteration number, you keep track of the previous value instead. The strange syntax is probably inspired by historical tokenization functions like strtok()

1 Like

it will function the exact same as pairs() in the example you posted but you can just use neither of those and do:

for k,v in tableReferenceHere do

end

That’s why I asked if I was using it Correctly

I usually see it used like this

for i, v in next, workspace:GetChildren() do

end

But most people just do this

for i, v in workspace:GetChildren() do

end

Found from this post

Ok i see,

So since it returns the next value, What does this code do?

I guess it’s probably the same as

for i, v in workspace:GetChildren() do

end

But I’ve never used it so I don’t know.

1 Like

The right half of a for-in statement consists of three things, some of which might be omitted:

for i, v in A, B, C do
end

A is an iterator function. “next” is the iterator function used in that example, it is also the first of three values returned by a call to pairs(). B and C are passed to the iterator function A on each loop. Calling pairs with a table ‘t’ returns (next, t, nil), which means the first set of (i, v) will be the result of calling next(t, nil), which gets the first element in the table. On the second loop, it calls next(t, v1), where v1 is the previously obtained value of v. pairs() is thus just a shorthand of filling out these three parts required for the loop.

In the code you quoted, C is not specified and is automatically assumed to be nil.

2 Likes

I see, but now I’m getting this error with next:
invalid Key to 'next'

Key is probably not in the table, which it needs to be. What code are you trying to run again?

I’m Trying both

local T = {

	[1] = "hi",
	[2] = "Hey"

}
local N = {"1","2","3","4","5"}
local P = {"i","p"}

local n = next(T, 1)
local n2 = next(T, 2)

print(n)
print(n2)
for p,i in  P, T, N  do
print(i)
end

for p,i in next, P, T, N  do
print(i)
end

I made this example of what is happening behind the scenes:

--Snore loops are the new thing!
local function Snore(bodyFunc, iteratorFunction, inputTable, firstKey)
	local i = firstKey
	local v
	
	while true do
		i, v = iteratorFunction(inputTable, i)
		if v == nil then break end
		bodyFunc(i, v)
	end
end


local tab = {
	"Boople",
	"Beeple",
	"Wonko"
}

BodyFunction = function(i, v)
	print(i ..": " ..v)
end

Snore(BodyFunction, next, tab, nil) --Iterate over tab using next as the iterator (the default), running BodyFunction for each loop
Snore(BodyFunction, next, tab) --Identical to the above

This mirrors what happens if you do:

for i, v in next, tab, nil do
	print(i ..": " ..v)
end

Yeah, I’m not sure what this example is supposed to be

(ah,i accidentally duplicated the code, sorry)

T is used correctly in your code.
P is a table but it actually needs to be a function that returns the next key and value, you should just use next.
N should be either nil or a key that exists in the table T.

for p,i in  next, T, nil  do
print(i)
end

Oh alright, thanks,

Note:
Please try to be less confusing, it is kind of difficult to follow what you are saying until I read it again
Although this is good, please try simplifying it after you explain what it is, to be a bit more clear for some people

The concept is already confusing as its a pretty abstract pattern and the Lua authors are famous for explaining these a bit too high-concept in their official explanations (ie in the PIL) I’m doing my best.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.