Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Outputting all values ??from an array in an array?

local array = {}
array[1] = {1,2,3,4,5}
array[2] = {5,4,3,2,1}

for i,v in pairs(array) do
	print(array[i][i])
end

This code outputs to output only 1 and 4. how do I output first the numbers from the first array (1,2,3,4,5) and then from the second, from the third one and so on

1 answer

So you have an array of arrays. To print each entry, you need two nested for-loops. That is, a for-loop inside a for-loop.

Also, since you want to print the value of each entry, not its key (also known as its index), you would want to print v instead of i.

local arrays = {}
array[1] = {1, 2, 3, 4, 5}
array[1] = {5, 4, 3, 2, 1}

-- Iterate through each array in
-- the list of arrays.
-- For clarity, we replace i with an underscore (_)
-- since we won't be using it anyway. 
for _, array in pairs(arrays) do
	-- Iterate through the array and print
	-- its value.
	for _, v in pairs(array) do
		print(v)
	end
end

1 comment

0

Made an edit, there was something wrong. Admittedly I didn't run this code before posting it.

This discussion is preserved as it was. New answers, comments, and votes are closed.