Multiple variables in a for statement

Hey, I know this may sound dumb, but I’m trying to make a script and I need to do “for x, y in z do” but I can’t figure out how. I am trying to make a mandelbrot fractal.

for x, y in z do
   doStuff()
end

This is vague. Are you wanting to mimic the functionality of for x, y in z: from Python? If so, just store every tuple pair in a table {{x, y}, ...} like you would in Python [(x, y), ...] and use pairs (or ipairs) to iterate through it.

vars = {{1, 2}, {3, 4}, {5, 6}}

for _, var in pairs(vars) do
  x, y = table.unpack(var)
  print(x, y)
end

>> 1 2
>> 3 4
>> 5 6

You can make your own iterator.

--!strict

local function setIter<T>(t: {{T}}): () -> ...T
	return coroutine.wrap(function(): ()
		for _: number, set: {T} in t do
			coroutine.yield(table.unpack(set))
		end
	end)
end

for n1: number, n2: number in setIter({{1, 2}, {3, 4}, {5, 6}}) do
	print(n1, n2)
end
2 Likes

Not sure what the format of your z table is, but if you’d simply like to snag every 2 elements of an array table z then you could just use the 3rd argument of a for loop:

for i = 1, #z - 1, 2 do
	local x = z[i]
	local y = z[i + 1]
	dostuff(x, y)
end

These could potentially be helpful: