WoW:API getn: Difference between revisions

From AddOn Studio
Jump to navigation Jump to search
(The # operator is NOT syntactic sugar, table.getn() is deprecated and # MUST be used)
m (Move page script moved page API getn to API getn without leaving a redirect)
 
(5 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{:Lua/Libshortcut|getn|table.getn}}
{{ambox
|border=red
|type='''Warning: This article discusses use of a deprecated function.'''
|style=margin-right: 10em
|info=*This function should no longer be used in Lua 5.1 and may be removed in the future.
*Use of it will result in potential future incompatibility.}}{{luaapi}}
From [http://lua-users.org/wiki/TableLibraryTutorial TableLibraryTutorial] of lua-users.org.
From [http://lua-users.org/wiki/TableLibraryTutorial TableLibraryTutorial] of lua-users.org.


  table.getn(table)
  table.getn(table)
getn(table)


{{band-red|'''Warning:''' This article discusses use of a deprecated function. This function should no longer be used in Lua 5.1 and may be removed in the future. Use of it will result in potential future incompatibility.}}


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


   > print( #{1,2,3} )
   > print( #{1,2,3} )
  3
However, it does not always behave as one might expect. Consider the following sequence:
> t = { 1, 2, 3 }
> dump (#t)
   3
   3


getn and the # operator start at index [1] and count up, ceasing when they reach a nil value. This means that if you have a table
> t[1] = nil
> dump (#t)
3


  t = { [0]      = "zero",   --will not be counted
  > dump (t)
      [1]      = "one",    --will be counted; 1
{
      [2]     = "two",    --will be counted; 2
   [2] = 2
      [3]     = "three",  --will be counted; 3
  [3] = 3
      ["four"] = "four",  --will not be counted; returns 3
} -- table: 0x807ff70
      [5]      = "five" } --will never be reached


running getn(t) or #t will return 3, not the expected 4. Although there are 4 indices, it will count to three, see that t[4] is a blank spot, and return with a count of three. It will never reach 5. Furthermore, keys such as t["four"] and array values less than 1 such as t[0] are not counted at all.
> dump (#{ [2] = 2, [3] = 3 })
0
As you can see, the # operator is unpredictable in that it can return different values for tables with identical contents. The Lua 5.1 reference manual has this to say about it [http://www.lua.org/manual/5.1/]:
The length of a table t is defined to be any integer index such that t[n] is not nil and t[n+1] is nil.
In other words, be careful with this operator if you are using tables with holes in them. If your table has all its values between [1] and [n] for some n, with no nils in that range, the # operator will return n. Otherwise its result may be unclear.
 
Note:
The reason the returned values are different is because the tables are different. For indexed tables, or tables that start with index values like 1,2,3 for keys, use tinsert and tremove to change the contents. What # returns is actually correct, setting the index value to nil does not change the size of the table for index keys, tremove would.


== See Also ==
== See Also ==
* [[tcount]]
* [[tcount]]
{{LUA}}

Latest revision as of 04:46, 15 August 2023

WoW Lua

From TableLibraryTutorial of lua-users.org.

table.getn(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 has switched to the # operator. #t is the replacement for table.getn(t)

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

However, it does not always behave as one might expect. Consider the following sequence:

> t = { 1, 2, 3 }
> dump (#t)
3
> t[1] = nil
> dump (#t)
3
> dump (t)
{
 [2] = 2
 [3] = 3
} -- table: 0x807ff70
> dump (#{ [2] = 2, [3] = 3 })
0

As you can see, the # operator is unpredictable in that it can return different values for tables with identical contents. The Lua 5.1 reference manual has this to say about it [1]:

The length of a table t is defined to be any integer index such that t[n] is not nil and t[n+1] is nil.

In other words, be careful with this operator if you are using tables with holes in them. If your table has all its values between [1] and [n] for some n, with no nils in that range, the # operator will return n. Otherwise its result may be unclear.

Note: The reason the returned values are different is because the tables are different. For indexed tables, or tables that start with index values like 1,2,3 for keys, use tinsert and tremove to change the contents. What # returns is actually correct, setting the index value to nil does not change the size of the table for index keys, tremove would.

See Also[edit]