WoW:API getn

From AddOn Studio
Revision as of 17:57, 30 November 2006 by WoWWiki>Casull (Added # operator)
Jump to navigation Jump to search

Lua/Libshortcut From TableLibraryTutorial of lua-users.org.

table.getn(table)

This is used to determine the size of a table. The size of a table is discussed at the top of this page.

> = table.getn({1,2,3})         -- Lua will count the elements if no size is specified
3
> = table.getn({1,2,3; n=10})   -- note, n overrides counting the elements
10
> t = {1,2,3}
> table.setn(t, 10)              -- set our own size with setn()
> = table.getn(t)
10
> = table.getn({1,2,3,nil,5,6}) -- sequence ends at element 3 due to nil value at 4
3

Note that Lua 5.1 supports the use of the # operator. #t is syntactic sugar for table.getn(t)

 > print( #{1,2,3} )
 3

See Also

Template:LUA