Lua: Finding the Length of a Table
Category Programming Techniques
The official documentation describes the #
operator as follows: The length operator is written as a unary #. The length of a string is its number of bytes (i.e., the length of the string calculated with one character per byte).
The length of a table t
is defined as an integer index n
. It satisfies that t[n]
is not nil and t[n+1]
is nil; additionally, if t[1]
is nil, n
could possibly be zero. For a regular array, when it contains some non-empty values from 1 to n
, its length is precisely n
, i.e., the index of the last value. If the array has a "gap" (meaning a nil value is sandwiched between non-nil values), then #t
might point to the index preceding any nil value (i.e., any nil value could be considered as the end of the array).
local tblTest2 =
{
1,
a = 2,
3,
}
print(table.getn(tblTest2))
What is the output of this code? The output here should be 2
. First, it is important to understand that tblTest2
is not a simple table; it mixes both list and record styles, with a = 2
being in record style. Secondly, records in record style are not counted as part of the table's length. You can think of it as a function, similar to other object-oriented languages, where functions are not counted as internal variables.
Since it is like a function, you can output the value of a
, yes. print(tblTest2.a)
will do.
Now, let's look at the following code:
local tblTest3 =
{
1,
{a = 2},
3,
}
print(table.getn(tblTest3))
What is the output of this code? The output here should be 3
. Note that nested tables are also elements. Therefore, the output is 3
.
table.getn(t)
is equivalent to #t
.
The following situations are quite tricky, so you can directly read the last sentence for a summary:
Now let's look at a more complicated one:
local tblTest4 =
{
1,
nil,
}
print(table.getn(tblTest4))
What is the output of this code? It is 1. We all know that when a table gets its length, it will traverse the entire table and return at the last non-nil position.
But what about the following code?
local tblTest5 =
{
1,
nil,
2,
}
print(table.getn(tblTest5))
It outputs 3, interesting, right? It counts nil as an element in the length. But what's even more confusing is the following code:
local tblTest5 =
{
1,
nil,
2,
nil
}
It outputs 1. Then, here's another one that will completely baffle you:
local tblTest5 =
{
1,
nil,
2,
nil,
3,
nil
}
It outputs 3, confused yet? Look at this one, which will make you never dare to write nil values in a Lua table again:
local tblTest5 =
{
1,
nil,
2,
nil,
3,
nil,
4,
nil
}
Look at this, the output is 1. Dear friend, I ask you, do you dare to use nil values in Lua tables in the future? If you continue to add nil values later, you might discover something. You might think you've found a pattern. But don't think you've found a pattern. Because that's wrong.
- Do not use nil in tables.
- If you must use nil, you must use the
table.setn()
function to set the length of the table. Note: Newer versions of Lua no longer supportsetn
.
- If you must use nil, you must use the
You must conclude: The setn
function is outdated, do not use nil values in Lua tables, if an element needs to be removed, just remove it directly, do not replace it with nil.
>
Source: http://www.cnblogs.com/youxin/p/3799339.html