How to get array size in LUA?

Here is the code:

users = {}  
users["aaa"] = "bbbb";
users["bbb"] = "bbbb";
users["ccc"] = "bbbb";
print("Users count ", table.getn(users));

Why does table.getn (users) always return 0? BTW, #users also returns 0. So, am I doing something wrong, and is there another way to get the number of elements in an array?

+5
source share
1 answer

table.maxn and # search for numeric indices; they will not see your string indexes.

As for getting the number of elements in an array with arbitrary indices, I would probably skip the array using something like:

Count = 0
for Index, Value in pairs( Victim ) do
  Count = Count + 1
end

but i'm an idiot.

+12
source

All Articles