table.getn
Returns the size of a numerically-keyed table
Prototype
n = table.getn (t)
Description
Returns the size of the table using the rules described at the start of this page.
A table's size (which is only relevant for "numeric" indexed tables) is one less than the first integer index with a nil value. If the table has "holes" in it - that is, numeric keys with gaps in the sequence - then the table size is not guaranteed to be the the last gap. Lua does a binary search to try to find a gap, it does not necessarily find the first or last one.
You can also use #t to find the length of a table.
A table's size (which is only relevant for "numeric" indexed tables) is one less than the first integer index with a nil value. If the table has "holes" in it - that is, numeric keys with gaps in the sequence - then the table size is not guaranteed to be the the last gap. Lua does a binary search to try to find a gap, it does not necessarily find the first or last one.
table.getn { "the", "quick", "brown", "fox", name = "Nick" } --> 4
Note that the length here is 4 and not 5, because the non-numeric entry (name = "Nick") is not considered to be part of the table's "length".You can also use #t to find the length of a table.
# { "the", "quick", "brown", "fox", name = "Nick" } --> 4
Contrast to table.maxn which finds the highest key by a linear scan (and is therefore slower).Lua functions
- table.concat - Concatenates table items together into a string
- table.foreach - Applies a function to each item in a table
- table.foreachi - Applies a function to each item in a numerically-keyed table
- table.insert - Inserts a new item into a numerically-keyed table
- table.maxn - Returns the highest numeric key in the table
- table.remove - Removes an item from a numerically-keyed table
- table.setn - Sets the size of a table (obsolete)
- table.sort - Sorts a table
Topics
- Lua LPEG library
- Lua PCRE regular expression functions
- Lua base functions
- Lua bc (big number) functions
- Lua bit manipulation functions
- Lua coroutine functions
- Lua debug functions
- Lua io functions
- Lua math functions
- Lua os functions
- Lua package functions
- Lua script extensions
- Lua string functions
- Lua syntax
- Lua table functions
- Lua utilities
- Scripting callbacks - plugins
- Scripting