Tables with names inside tables

So I have created a Lua program to keep track of the needs of a property my father is buying, and I want to make tables inside tables WITH NAMES. So when I try to add it through a function I created, (I'll show the function), it expects ) where "=" is.

--The table I'm using to store everything
repair={}
--The function I'm using to create tables inside tables
function rAdd(name)
 table.insert(repair, name)
end
--The function I'm using to add data to those tables
function tAdd(table, name)
 table.insert(table, name)
end
rAdd(wall={})
tAdd(wall, "Due for paint job")

And when I try to add it (rAdd(wall={})) it expects me to end the argument via ) at the "=". Please help!

Upvotes: 2

Views: 50

Answers (1)

Bartek Banachewicz
Bartek Banachewicz

Reputation: 39390

Instead of mocking around with table.insert, just use the fact Lua tables can be accessed like, well tables:

repair["wall"] = {}

Now you can actually insert into it:

table.insert(repair["wall"], "Due for a paint job")

If you want to hide the global variable behind a function:

function rAdd(name, value)
    repair[name] = value
end

rAdd("wall", {});

Or if you really want to pass entries in the table form:

function rAddN(entries)
    for k,v in pairs(entries) do
        repair[k] = v
    end
end

rAddN({ wall = {} })

Note that you can omit parens in this case:

rAddN { wall = {} }

Upvotes: 2

Related Questions