local Table = {['TableName] = {}} print(Table.TableName)
Will print table
bt i want it to print TableName
You’re getting an output of Table
because the index TableName
points to an array. The Table you have formatted is called a dictionary
, this format allows people to to add variable pointers as forms of reference to data within the array. The variable name will not correlate to the actual name, only the data; the same behaviour as calling print
and passing a variable. However, unlike generic variables , you can actually get the reference name through the index marker that is returned by a pairs
loop.
local Table = { ["TableName"] = {}; ] for ReferenceMarker,_ in pairs(Table) do print(ReferenceMarker) end
You are printing TableName however you might have confused it for 'Table', since both outputs would yield a table being printed.
This piece of code I added can prove it.
local Table = {[TableName] = {"foo","bar"}} print(Table.TableName[1])
Output:
foo
And sure enough; you are indeed referencing TableName
.
If you want to print all the elements in the TableName table you can use the following code:
local Table = {[TableName] = {"foo","bar"}} for i,v in pairs(Table.TableName) do print(v) end
Output:
foo
bar