So I wanted to know how to get the index of a dictionary item. I’ve tried this code:
local testDict = {
["Item"] = {
-- this should equal 1 as a returned index
},
["OtherItem"] = {
-- this should equal 2 as a returned index
}
}
for i, v in pairs(testDict) do
local returnedIdx = table.find(testDict, i)
print(returnedIdx)
end
but all values returned were nil… Any alternatives? Or am I using table.find() wrong?
In Lua, dictionaries aren’t ordered, meaning key/value pairs have no numerical order or association. The “i” variable gives you the current step of the loop, but it’s not related to the key/value pair.
Your suggestion did work (I changed the script to convert it to an array)
local testDict = {
["Item"] = {
-- this should equal 1 as a returned index
},
["OtherItem"] = {
-- this should equal 2 as a returned index
}
}
local testArr = {}
for i, v in pairs(testDict) do
table.insert(testArr, i)
end
print(testArr)
for i=1, #testArr do
-- Do stuff?
end