How do I get the number of keys in a hash table in Lua?

I experimented with both the # operator and table.getn(). I thought table.getn() would do what you wanted but as it turns out it’s returning the same value as #, namely 0. It appears that dictionaries insert nil placeholders as necessary.

Looping over the keys and counting them seems like the only way to get the dictionary size.

Leave a Comment