anthlons
(00anthony)
June 21, 2019, 2:17am
#1
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…
posatta
(pasta)
June 21, 2019, 2:18am
#2
If you don’t know the key, you shouldn’t be using a dictionary.
1 Like
anthlons
(00anthony)
June 21, 2019, 2:19am
#3
What should I use besides a dictionary? Not sure of anything else there is…
posatta
(pasta)
June 21, 2019, 2:21am
#4
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
anthlons
(00anthony)
June 21, 2019, 2:24am
#5
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
posatta
(pasta)
June 21, 2019, 2:28am
#6
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