957 posts
Location
Web Development
Posted 20 February 2015 - 06:41 PM
Ok so lets say I have a 'defualt table'
local defualt = {
name = "test",
height = 1,
width = 1,
state = true
}
I want to write a function that accepts a table as an arguemnt, then returns that table with all the missing variables filled in from the defualt table
function fillTable(info)
--#fill in missing stuff
return info
end
temp = {
name = "bob"
}
temp = fillTable(temp)
Temp should now be
{
name = "bob",
height = 1,
width = 1,
state = true
}
How would I go about doing that?
1426 posts
Location
Does anyone put something serious here?
Posted 20 February 2015 - 06:56 PM
I would instinctively do something like:
local merge(a, B)/>
for k,v in pairs(B)/> do
a[k] = v
end
return a
end
local defaults(a)
return merge({
name = "test",
height = 1,
width = 1,
state = true
}, a)
end
Another way would do it would be this:
local defualt = {
name = "test",
height = 1,
width = 1,
state = true
}
local defaults(a)
for k,v in pairs(default)
if a[k] ~= nil then
a[k] = default[k]
end
end
return a
end
3057 posts
Location
United States of America
Posted 20 February 2015 - 06:56 PM
local defualt = {
name = "test",
height = 1,
width = 1,
state = true
}
function fillTable( tbl )
for k, v in pairs( default ) do
tbl[ k ] = tbl[ k ] or v
end
return tbl
end
This will work on everything except boolean inputs, ei state. state will always be set to true unless a non-nil, non-boolean value is in tbl.
Edit:Ninja'd
Edited on 20 February 2015 - 05:56 PM
957 posts
Location
Web Development
Posted 20 February 2015 - 07:01 PM
Spoiler
I would instinctively do something like:
local merge(a, B)/>/>
for k,v in pairs(B)/>/> do
a[k] = v
end
return a
end
local defaults(a)
return merge({
name = "test",
height = 1,
width = 1,
state = true
}, a)
end
Another way would do it would be this:
local defualt = {
name = "test",
height = 1,
width = 1,
state = true
}
local defaults(a)
for k,v in pairs(default)
if a[k] ~= nil then
a[k] = default[k]
end
end
return a
end
Spoiler
local defualt = {
name = "test",
height = 1,
width = 1,
state = true
}
function fillTable( tbl )
for k, v in pairs( default ) do
tbl[ k ] = tbl[ k ] or v
end
return tbl
end
This will work on everything except boolean inputs, ei state. state will always be set to true unless a non-nil, non-boolean value is in tbl.
Edit:Ninja'd
Thanks guys :)/>
355 posts
Location
Germany
Posted 20 February 2015 - 07:40 PM
Another way would be to set the __index metamethod to the default table:
function addDefaults(x)
return setmetatable(x,{__index = defaults})
end
This way every lookup on the table that would end in nil, would be first looked up in the defaults table, and only if theres no entry for the requested index also, it'll return nil, otherwise the default value. It's like prototyping everything in the defaults, if it's not set in the table x and it'll influence x until the metatable is removed/overriden. I consider this somewhat cleaner and your case maybe even faster, as you don't do hell-a-lot of indexing on the returned table. (I would assume it's for your simple button thingy, if i remember correctly)
957 posts
Location
Web Development
Posted 20 February 2015 - 08:05 PM
Another way would be to set the __index metamethod to the default table:
function addDefaults(x)
return setmetatable(x,{__index = defaults})
end
This way every lookup on the table that would end in nil, would be first looked up in the defaults table, and only if theres no entry for the requested index also, it'll return nil, otherwise the default value. It's like prototyping everything in the defaults, if it's not set in the table x and it'll influence x until the metatable is removed/overriden. I consider this somewhat cleaner and your case maybe even faster, as you don't do hell-a-lot of indexing on the returned table. (I would assume it's for your simple button thingy, if i remember correctly)
You are correct, its for
my Screen API.
I think Squid and King's answers pretty much covered what I need, but this is good to know about
Edited on 20 February 2015 - 07:24 PM