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

How do I print the names of tables?

Asked by 5 years ago

Let's say I have this table:

local Rarities = {
    ["Common"] = {"Oof1", "Oof2"},
    ["Common2"] = {"Oofa", "Oofb"},
}

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

of course, this would print something like Table:9B9B9B9B

What I would like simply is to output "Common".

1 answer

Log in to vote
5
Answered by 5 years ago
Edited 5 years ago

If you simply want the indexes, then you should be printing i, not v If you needed to print in the values inside of i, then you would simply unpack it.

The reason why it is printing table's memory location is because

I) You are printing the value of index, not the index itself

II) Simply because you are printing the table, which if you wanted to get it's value, you would have to unpack it. You have a matrix. That is why.

local Rarities = {
    ["Common"] = {"Oof1", "Oof2"},
    ["Common2"] = {"Oofa", "Oofb"},
}

for i, v in pairs(Rarities) do
    print(i)
    print(unpack(v));
end

would print

--[[
Common2
Oofa      Oofb
Common
Oof1      Oof2
]]

Edit: Whoops forgot Lua 5.1 uses unpack not table.unpack.

1
Works like a dream! Thank you! Marmalados 193 — 5y
Ad

Answer this question