Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do you get the Variable name of the table?

Asked by 4 years ago
Edited by Ziffixture 4 years ago
local Table = {['TableName] = {}}
print(Table.TableName)

Will print table

bt i want it to print TableName

2 answers

Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

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
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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

Answer this question