Why lua returns 0 with a table?

there is this feature that lets you make a table using strings
ex:

local foo = {
    bar = {...}
    foo = true
}

when i do #foo then it returns 0

do anyone have alternatives to check the table’s length?

Well, first of all, a comma is needed between two values ​​in the table:

local foo = {
	bar = {...}, -- there
	foo = true
}

And here’s the bug:

2 Likes

What you created is called a dictionary, which is a table where each value in it is indexed by a key. Dictionaries are unordered, and do not have a length you can get by using the # operator on them.

If you want to get the length of a dictionary, you have to iterate over it like this:

local function getDictionaryLength(dict)
    local length = 0
    for key, value in dict do
        length += 1
    end

    return length
end
2 Likes

thanks for pointing my mistake😅
since i typed the table out on my phone, i didnt even notice that i forget to put a comma in there.

i think this is only the only possible solution to the question, also thanks for the explaination

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.