Is there a way to make it not happen?

Hello,how can I print a table without making it print something like this: table: 0xc16d4e992915e177

local object = {
   thing = "something"
}

print(object)

-->table: 0xc16d4e992915e177 //this is not what I wanted

this is what I want to see

local object2 = {
   thing = "something"
}

print(object)

-->object2 //this is what I want to see (something like this)

Sorry If this question might be silly for you but I’m not an expert (yet) at this langauge.

I dont really use print on tables often but i believe the “table: 0xc16d4e992915e177” will only happen if its a dictionary

for your solution it would be

for i, v in pairs(object2) do
print(i, v)
end

should print “thing, something”

since for i, v will go through all the keys and values

the problem is that I want the print() function prints the name of the table

unless im mistaken but i dont think it will print the name of the table unless you put a variable within the table like

local T = {
      Name = "TableName"
} 
1 Like

So you want it to literally print object2 into the output? Like the name of the table
Unless you store the name of the table within the table, like:

local object2 = {
     thing = "something",
     name = "object2",
}
print(object2.name)

that’s one way around it, albeit hacky (it appears this has just been said, so if this is your answer please mark the post above as solution)

If you are wanting the contents of the table to print, make sure this option is toggled:
image
“Show tables expanded by default” in your output

or use table.unpack(object2) and print that.

You can use the __tostring metamethod:

local object2 = setmetatable({
   thing = "something"
}, {
    __tostring = function()
        return "object2"
    end
})

print(object2) -- "object2"

Basically what __tostring does is whenever you try to convert the table into a string with tostring, it will return whatever is specified in the return function, which prints out “object2” in this case because we set it to return that. Print statements tostring everything, so that’s why this works.

(source)

Thank you for answering this question,but I didn’t wanted that it prints a string code,I needed that it prints the name of the table

What’s the point of this? You’re already putting the name of the table into the print in order to print the table in the first place.