Reputation: 634
I have a table t which is totally dynamic: it is nested, and number of levels is unknown in advance. I wish to be able to set (get is easy to do) some value at some level, using a kind of path. The path would be an array (this doesn't have to be, just an idea, but seems to make sense to me)
Here is a kind of code, which obviously doesn't work (it will always return 3), but gives an idea of what I need:
t={a={b={c=3}}}
path={"a","b","c"}
r=setmetatable(t,
for _,i in pairs(path) do
r=r[i]
end
r=1
print(t["a"]["b"]["c"])
I thought of using __index, __newindex and metatable, but I still struggle to use those, and all my experiments failed Otherwise, getting the address of the table, rather than its value would be the solution, but I don't know how to do that...
Upvotes: 0
Views: 91