Sorry for the vague title, I’ll try to explain this well.
What I want to do is this:
abc = {
a = "1",
b = "2",
c = "3"
}
print(abc[1])
The problem with this is that you can’t use [] in a dictionary, is there a way to do this?
And no, I can’t do something like print(abc.a), because everything has the same name in the dictionary.
That wouldn’t really work though, as I’m using table.insert to add another table, do you know if there’s a way to give something like this a unique number?
If everything has the same name, it is not a dictionary. A dictionary has different keys, always. You are either not using a dictionary, or overwriting the same key multiple times.
The issue with this is that the order in which a dictionary is traversed is undefined.
local function getNthValueInDict(t, n)
local index = 1
for _, v in pairs(t) do
if index == n then
return v
end
index += 1
end
end
local abc = {a=1, b=2, c=3}
print(getNthValueInDict(abc, 2)) --3
print(getNthValueInDict(abc, 2)) --3
print(getNthValueInDict(abc, 2)) --3
The order of dictionaries like this is not guaranteed to stay the same. From what I can remember inserting / removing items will affect the order in which values are iterated.