How to get the place of a dictionary in a dictionary | #2

Adding onto my other thread: How to get the dictionaries in a dictionary?

I know you can see how many keys are in the dictionary, but how would I get the place?
Code:

local Config = {}
Config["Questions"] = {
	["Hi"] = {
		"Hola",
	},
	["Hi2"] = {
		"Hey man",
	},
} 

How would I get “Hi” if I do not know the key? Config[“Questions”][1] Doesn’t work as It well… doesnt work…

If you don’t know the key, you shouldn’t be using a dictionary.

1 Like

What should I use besides a dictionary? Not sure of anything else there is…

An array. You can define it by just putting your tables like this:

Config["Questions"] = {
	{ "Hola" };
	{ "Hey man" };
}

Then you can access elements with Config["Questions"][1], Config["Questions"][2], and so on.

3 Likes

Inside

Config["Questions"] = {
	{ "Hola" };
	{ "Hey man" };
}

Could I do something like:

Config["Questions"] = {
	{ Name = "Hola", Questions = {
   "Blah"
} };
	{ Name = "Hey man", Questions = {
   "Blah1",
   "Blah2"
} };
}

Then get Name without actually knowing name?

2 Likes

If you’re not sure, you can always just test it in studio or at the lua site (for non Roblox-specific code). That should work fine though, yes.

1 Like

Just to add onto the solution, if you want to be more explicit for clarity’s sake, you could put the number index in there manually:

local Config = {}
Config["Questions"] = {
	[1] = {"Hola"},
	[2] = {"Hey man"}
}

As long as the numbers are incremented by 1s from 1 to #Table, it will still be considered an array, and objects can still be iterated through in order with pairs and ipairs.

2 Likes