Corona Tables

Hi there,

I am trying to set a value in an multidimensional array.

local t = { name = "foo", login = "foo", score = { classic = { easy = 0, normal = 0, hard = 0 } } }

Is there any way to directly set the value t if the indeces are variable?

So i want to set the value with a function call like 

function setValue(name, value) if type(name) == "string" then elseif type(name) == "table" then -- i want to be able to set the value t.score.classic.easy -- with the given table {"score", "classic", "easy"} end end

any ideas?

thanks

Don’t think a function is necessary. I was able to properly set the value by using the following command:

t["score"]["classic"]["easy"] = 5

Ah ok I think I made myself not completly clear.

score, classic, and easy are variables

I pass a table to the function and set the value

function setValue(name, value) if type(name) == "string" then elseif type(name) == "table" then -- i want to be able to set the value t.score.classic.easy -- with the given table {"score", "classic", "easy"} end end setValue({"score", "classic", "easy"}, 10) setValue({"score", "classic", "normal"}, 10) setValue({"score", "classic", "hard"}, 10)

I see what you’re trying to do, not sure why though? After all, this:

[lua]

t.score.classic.easy = 10

[/lua]

is shorter than:

[lua]

setValue({“score”, “classic”, “easy”}, 10

[/lua]

local function setValue( table, value ) if type( table ) == "table" and #table == 3 then local arg1, arg2, arg3 = table[1], table[2], table[3] t[arg1][arg2][arg3] = value else print( "Must pass 3-element table as first argument" ) end end

If you really need a function, this is it, and it’s going to come out to the same thing as Nick and I wrote anyway.

I use this logik in a reusable library and I thought there was a more dynamic way to solve the problem. 

Could have been I missed some metatable trickery.

But the fact that it is not really possible is good too =D

Thanks.

Don’t think a function is necessary. I was able to properly set the value by using the following command:

t["score"]["classic"]["easy"] = 5

Ah ok I think I made myself not completly clear.

score, classic, and easy are variables

I pass a table to the function and set the value

function setValue(name, value) if type(name) == "string" then elseif type(name) == "table" then -- i want to be able to set the value t.score.classic.easy -- with the given table {"score", "classic", "easy"} end end setValue({"score", "classic", "easy"}, 10) setValue({"score", "classic", "normal"}, 10) setValue({"score", "classic", "hard"}, 10)

I see what you’re trying to do, not sure why though? After all, this:

[lua]

t.score.classic.easy = 10

[/lua]

is shorter than:

[lua]

setValue({“score”, “classic”, “easy”}, 10

[/lua]

local function setValue( table, value ) if type( table ) == "table" and #table == 3 then local arg1, arg2, arg3 = table[1], table[2], table[3] t[arg1][arg2][arg3] = value else print( "Must pass 3-element table as first argument" ) end end

If you really need a function, this is it, and it’s going to come out to the same thing as Nick and I wrote anyway.

I use this logik in a reusable library and I thought there was a more dynamic way to solve the problem. 

Could have been I missed some metatable trickery.

But the fact that it is not really possible is good too =D

Thanks.