table.Count
Example
There are 4 keys in this table. So it will output "4"
Output:
4
Example
Difference between the length (#) operator and this function.
The length (#) operator is generally considered faster, but has limitations.
local Table = { A = "1", B = "2", C = "3", D = "4" }
print( table.Count( Table ), #Table ) -- #Table will return 0 because the table contains no numeric keys
local Table2 = { "test1", "test2", "test3" } -- 1 = "test1", 2 = "test2"
print( table.Count( Table2 ), #Table2 ) -- Both will be 3
Table2[ 5 ] = "test5" -- Insert a new value at index 5, so index 4 does not exist
-- table.Count here will return correct value, #Table2 will return 3 because
-- the new value is non sequential ( there is nothing at index 4 )
print( table.Count( Table2 ), #Table2 )
Output:
4 0
3 3
4 3