How to index a table with a tuple and change the value?

Is it possible to have a tuple of keys, and index a table with this tuple somehow to change the table value? If not, are there any alternatives that achieve the same effect?

Using this table and tuple

local table = {}
… = “Key1”, “Key2”, “Key3”

To do this (Assuming the table has the keys defined):

table[“Key1”][“Key2”][“Key3”] = “Some New Value”

So basically, when given a tuple of keys, can I index a table with these keys and change the value?

(A for loop wouldn’t work, because we want to change the value and keep the original reference, not just access it)

2 Likes
local keys = {...}
local t = original_table
for i = 1, #keys-1 do
  t = t[keys[i]]
end
t[keys[#keys]] = value_you_want_to_set

This should work, I think I did this once before.

5 Likes