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

For loop retrieving a dictionary within a table?

Basically what I have been trying to do is get all the keys in a dictionary and what their value is, however, those dictionaries are in an array so I don't know how to retrieve them from the for loop in ipairs.

local codes = {}
codes[1] = {
	Code = "TestCode",
	Expires = false
}
codes[2] = {
	Code = "ExpiringCode",
	Expires = true,
	ExpYear = "2020",
	ExpMonth = "11",
	ExpDay = "8",
	ExpTime = "13:08"
}
for key, value in ipairs() do --What to put in pairs()?
	print(key.. ": ".. value)
end

I have already tried to put codes[2] in the for loop and it did not work.

Thank you for taking the time to read this, it is very much appreciated! :D

1 comment

0

You need to do a double for loop. So for i,v in pairs(value) inside of the original for key,value in pairs.

1 answer

✓ Accepted answer

To iterate through an array made of arrays you would need a nested loop; The first one would go through the arrays and the second would go through each individual array inside of it.

But the reason you just iterating through codes[2] didnt work is because its a dictionary and you tried to use ipairs

ipairs can only be used for indexed arrays, meaning that the keys are numeric. To loop through a dictionary you can use pairs.

pairs would work for both dictionaries and arrays, but ipairs is slightly faster so for arrays its best you use that.

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