I currently have this code, but I feel this can be optimized much better:
local config = {
val = 1,
background = {
foo = 5,
textures = {
bar = 'okay',
layers = {
alpha = 1,
color = {
red = 255,
blue = 0,
green = 100
}
}
}
},
sub = {
val = 5,
foo = {
val = true,
bar = {
}
}
}
}
local function set_config(...)
local arg = {...}
if type(arg[1]) == 'table' then
table.extend(config, arg[1])
elseif #arg == 2 then
if type(arg[2]) == 'table' then
table.extend(config[arg[1]], arg[2])
else
config[arg[1]] = arg[2]
end
elseif #arg == 3 then
if type(arg[3]) == 'table' then
table.extend(config[arg[1]][arg[2]], arg[3])
else
config[arg[1]][arg[2]] = arg[3]
end
-- And now for the 4th time. This is starting to get way to repetitive
elseif #arg == 4 then
if type(arg[4]) == 'table' then
table.extend(config[arg[1]][arg[2]][arg[3]], arg[4])
else
config[arg[1]][arg[2]][arg[3]] = arg[4]
end
-- And for what I try to achieve, I should continue this elseif's until indefinite
end
end
-- And this should go as deep into the table as I want to
Now I can do
set_config('val', 3)
set_config('sub', {
foo = false
})
But I want to go as deep into the table without the limit I have now with the set_config
So I could do
set_config('background', 'textures', 'layers', 'medium', 'color', 'red', 255)
And for clarification, the table.extend
function table.extend(t1, t2)
for k, v in pairs(t2) do
if (type(v) == "table") and (type(t1[k] or false) == "table") then
table.extend(t1[k], t2[k])
else
t1[k] = v
end
end
return t1
end
I've tried out a few things, but my Lua knowledge is rather limited.
The answer by lhf was almost correct, here is the outcome that works as i wanted it:
local function set_config(...)
local a={...}
local n=#a
local t=config
if #a == 1 and type(a[1]) == 'table' then
table.extend(config, a[1])
return
end
for i=1,n-2 do
local k=a[i]
t[k]=t[k] or {}
t=t[k]
end
if type(t[a[n-2]]) == 'table' then
table.extend(t[a[n-1]], a[n])
else
t[a[n-1]] = a[n]
end
end
configis acting like the 'root' of the table. What canconfighave as keys and values? Your example above is suggesting 2 but can it also have arbitrarily many keys?