table.getn
Lua function

table.getn

Summary

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.


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).



See Also ...

Topics

DOC_lua_base Lua base functions
DOC_lua_bc Lua bc (big number) functions
DOC_lua_bit Lua bit manipulation functions
DOC_lua_coroutines Lua coroutine functions
DOC_lua_debug Lua debug functions
DOC_lua_io Lua io functions
DOC_lua_math Lua math functions
DOC_lua_os Lua os functions
DOC_lua_package Lua package functions
DOC_lua_rex Lua PCRE regular expression functions
DOC_lua Lua script extensions
DOC_lua_string Lua string functions
DOC_lua_tables Lua table functions
DOC_lua_utils Lua utilities
DOC_scripting Scripting

Lua functions

LUA_table.concat table.concat (Concatenates table items together into a string)
LUA_table.foreach table.foreach (Applies a function to each item in a table)
LUA_table.foreachi table.foreachi (Applies a function to each item in a numerically-keyed table)
LUA_table.insert table.insert (Inserts a new item into a numerically-keyed table)
LUA_table.maxn table.maxn (Returns the highest numeric key in the table)
LUA_table.remove table.remove (Removes an item from a numerically-keyed table)
LUA_table.setn table.setn (Sets the size of a table (obsolete))
LUA_table.sort table.sort (Sorts a table)

(Help topic: lua=table.getn)

DOC_contents Documentation contents page