Jump to content




Default table


  • You cannot reply to this topic
5 replies to this topic

#1 HPWebcamAble

  • Members
  • 933 posts
  • LocationWeb 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?

#2 SquidDev

    Frickin' laser beams | Resident Necromancer

  • Members
  • 1,427 posts
  • LocationDoes 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


#3 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited 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 by KingofGamesYami, 20 February 2015 - 06:56 PM.


#4 HPWebcamAble

  • Members
  • 933 posts
  • LocationWeb Development

Posted 20 February 2015 - 07:01 PM

View PostSquidDev, on 20 February 2015 - 06:56 PM, said:

Spoiler

View PostKingofGamesYami, on 20 February 2015 - 06:56 PM, said:

Spoiler


Thanks guys :)

#5 InDieTasten

  • Members
  • 357 posts
  • LocationGermany

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)

#6 HPWebcamAble

  • Members
  • 933 posts
  • LocationWeb Development

Posted 20 February 2015 - 08:05 PM

View PostInDieTasten, on 20 February 2015 - 07:40 PM, said:

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 by HPWebcamAble, 20 February 2015 - 08:24 PM.






3 user(s) are reading this topic

0 members, 3 guests, 0 anonymous users