Find table length in lua

The official documentation describes it like this: The length operator is written as the unary operation #. The length of a string is its number of bytes (that is, the length of a string calculated by one character and one byte).

The length of table t is defined as an integer subscript n. It satisfies that t [n] is not nil and t [n + 1] is nil; moreover, if t [1] is nil, n may be zero. For regular arrays, when some non-null values ​​are placed from 1 to n, its length is exactly n, which is the index of the last value. If the array has a "hole" (that is, nil values ​​are sandwiched between non-null values), then #t may be an index pointing to any previous position that is a nil value (that is, any nil value is May be considered the end of an 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 of all, we must understand that this ctblTest2c is not a simple ctable, it mixes two styles of list (list) and record (record). In the table, a = 2 is record style. Secondly, understand that record-style records are not calculated as the length of the appearance. You can think of it as a function. Like other object-oriented languages, functions are not recorded as internal variables.

Since it is like a function, it can output the value of a, yes. print (tblTest2.a) is fine.

Look at the following code again:

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 tables are nested tables, and nested tables are also elements. So, the output is 3.

table.getn (t) is equivalent to #t.
The following situations are quite tangled, you can directly read the last sentence summary:

Now look at a more tangled:

local tblTest4 =
{
     1,
     nil,
}

print (table.getn (tblTest4))

What is the output of this code? it's 1. We all know that when the table gets the length, it will traverse the entire table and return at the last non-nil place.
But what about the following code?

local tblTest5 =
{
     1,
     nil,
     2,
}

print (table.getn (tblTest5))
Output 3, isn't it fun? It also uses nil as an element to calculate the length. But what makes you scratch your head is the following code:
local tblTest5 =
{
     1,
     nil,
     2,
     nil
}

Output 1. Send another paragraph to make you completely blind:

local tblTest5 =
{
     1,
     nil,
     2,
     nil,
     3,
     nil
}

Output 3, have you lost it? Again, this paragraph tells you that you will never dare write nil values ​​in the table in the future:

local tblTest5 =
{
     1,
     nil,
     2,
     nil,
     3,
     nil,
     4,
     nil
}

Take a look, the output of this paragraph is 1. Dear, may I dare to use nil value in lua's table in the future? ? ? If you continue to add nil later, you may find something. You may think that what you find is a pattern. However, you should never think that this is a rule. Because this is wrong.

1.Do not use nil in the table

2. If you have to use nil, you must use the table.setn () function to set the length of this table. Note: The new version of Lua no longer supports setn.

I must give you a conclusion: the setn function is obsolete. Do not use nil values ​​in lua's table. If an element is to be deleted, remove it directly and do not use nil instead.

Author: