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

How can I determine the size of a table?

Asked by 6 years ago

Hi guys,

I have a table with ~150 items in it and I was wondering how I can count the number of entries within the table?

Am I being dumb and using print(#items) wrong? or does that only work for arrays?

I've also tried:

NoCreatures = function()
    local count = 0

    for _ in pairs (items) do 
        count = count + 1
    end

    print(count)
end

I'm sure there's a simple solution to this one! haha

Thanks for your time :)

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Arrays are tables with a number as the key in Lua so #array should work.

0
Does it matter that I have made it like --> Items["#092"]="Example" ? AlexTheCreator 461 — 6y
0
Items = {} is at the beggining of it all. and I know it looks stupid to have it as #000 instead of just letting it be an array but there's a reason haha (e.g. the final number, #150 is only about number 146) AlexTheCreator 461 — 6y
0
Not at all since "#092" is being treated as a key. Though you can't iterate through it with numbers unless you do something semicomplex.. Meltdown81 309 — 6y
0
Thanks for the help :) AlexTheCreator 461 — 6y
Ad

Answer this question