FANDOM


WARNING
The functionality described below may not be safe to use in some or many cases.
Read all warnings and technical notes and understand all possible consequences before use.
count( tbl )
Count the non-nil entries in an array-style table
tbl
The table.

This legacy, undocumented function simply iterates over the indices in an array-style table and counts how many are non-nil.

Note that this will not count entries which are not numeric array-style. See the example below for a demonstration.

This function was removed from the official API as of version 0.1.2:

removed count() from docs -- now just a legacy function. Use # operator instead.

It may be worth noting that the advice to use the # operator instead is probably well-intentioned, but the two operations are slightly different. The # operator returns the highest non-nil element's index, while this function laboriously walks over the table's contents and counts everything that isn't nil. In a full table, these are the same number, but with nil elements, they will differ.

Warning Edit

As a now-undocumented legacy function, count() cannot be relied on for a game. Its functionality may be removed in future versions. Still, some may find a pragmatic use for it in tools or demos. Just be aware that you use it at your own risk.

Examples Edit

> t = {1,2,nil,4,nil,6,7}
> print(count(t))
5
 
> -- this is not a numeric array-style element and will not be seen by count(t)
> t.x = 123
> print(count(t))
5
 
> -- this is not a numeric array-style element and will not be seen by count(t)
> t[true] = "true"
> print(count(t))
5

See also Edit

Community content is available under CC-BY-SA unless otherwise noted.