Reputation: 11
I don't really understand why this doesn't create a table using a given name. Can someone help clarify this for me?
local table={}
local function createtable(tabname, propername)
table.tabname={}
table.tabname.propername=propername or "need a proper name"
end
createtable(foo, "first table")
createtable(bar, "second table")
for k,v in pairs(table) do
print("table name: "..k)
end
Output:
table name: tabname
Why doesn't it use the given variable value in the function call?
Upvotes: 0
Views: 66
Reputation: 80921
You meant tab[tabname]={}
not tab.tabname={}
.
The tab.str
syntax treats the bit after .
as a string key not a variable.
Also don't use table
as a table name. You shadow the default table
library.
Also in createtable(foo, "first table")
unless the foo
variable already exists that is equivalent to createtable(nil, "first table")
. Did you mean createtable("foo", "first table")
?
Upvotes: 2