I think what you want is
setmetatable. Especially the
__newindex metamethod to map values as keys on creation instead of that traversing via a manual loop.
However keep in mind __newindex will only trigger if that metatable has been added and the way you're defining it right now gives it now way to trigger __newindex ever so you will need to use a virtual function like this to get it to work and keep adding it as a collection:
Code:
function mapped_table(t)
local ret = setmetatable({}, {
__newindex = function(self,k,v)
rawset(self,k,v) -- map index as key, return value (default)
rawset(self,v,k) -- map value as key, return index
end
})
for k, v in pairs(t) do
ret[#ret+1] = v
end
return ret
end
local myWhatEverMaterialTable = {}
myWhatEverMaterialTable[8] = mapped_table({
1245,
1259872,
25115,
2958257,
21587,
2187
})
Code:
d(myWhatEverMaterialTable[8][25115]) -- outputs index: 3
d(myWhatEverMaterialTable[8][3]) --outputs value: 25115
d(myWhatEverMaterialTable[8][666]) -- outputs nil because neither the index noir the value exists in the table
Edit:
Also never use table.insert because it 's ignoring metatables as well.
Set them manually and you're good to go (like with my function above).