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

Using # in a table counts other tables inside?

Asked by 3 years ago
Edited 3 years ago

When I put a # in front of my table name, it errors because of the code in the else of the if statement. My table consists of something like this:

local erroringTable = {["s1"]={["id"]="",["desc"]="Description"}}

and when I put a hashtag in front of it,

print(#erroringTable)

is way less than it should be! It's 0! If you the answer, please reply. Thanks!

1
You can't count the length of a hashtable. Ziffixture 6913 — 3y

1 answer

Log in to vote
0
Answered by
sleazel 1287 Moderation Voter
3 years ago

To count hash table (dictionary), you need to iterate through all keys:

local function Count(table)
    local count = 0
    for _, _ in pairs(table) do
        count = count + 1
    end
    return count
end

local erroringTable = {["s1"]={["id"]="",["desc"]="Description"}}

print(Count(erroringTable))
Ad

Answer this question