0

I want the key pairs to be tableToPopulate.width = 30 and tableToPopulate.Height = 20 They are currently tableToPopulate[1] = 30 and tableToPopulate[2] = 20


local function X ()
    code, code...
    return 30,20
end

local tableToPopulate = {
    x()
}
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
Gullie667
  • 91
  • 6

2 Answers2

4

Why don't you just return a table?

local function X ()
    return {width=30, height=20}
end
proxi
  • 1,243
  • 10
  • 18
1

You could pass in the table you want the values set on, like this:

function x(tbl)
    tbl.Height = 20; 
    tbl.Width = 30;
end

local t={}
x(t)
print(t.Height, t.Width)

although it may make more sense to use nested tables depending on how complex the structure will be of whatever is in the table.

function x(tbl)
    table.insert(tbl, {Height = 20, Width = 30})
end

local t={}
x(t)
print(t[1].Height, t[1].Width)

which would be equivalent to this:

function x()
    return {Height = 20, Width = 30}
end
local t = {x()}
print(t[1].Height, t[1].Width)

So really, it depends on how you want to group the data and which syntax you prefer.

Mike Corcoran
  • 14,072
  • 4
  • 37
  • 49