Lua - table 的大小返回不同
Lua - Size of table returning different
伙计们。有人可以帮我解决这个问题吗?
输入
a = {}
a.c = {1,2,3}
print(#a)
print(a.c)
输出
0
table: 0x11ed7a0
为什么#a 是 0?为什么不是 1?
谢谢。
Lua table 与其他语言中的结构不同。如 Lua manual 所示:
Tables are the main (in fact, the only) data structuring mechanism in Lua, and a powerful one. We use tables to represent ordinary arrays, symbol tables, sets, records, queues, and other data structures, in a simple, uniform, and efficient way.
此外,table 具有足够的动态性,您可以同时以多种方式使用它。例如,table 可以同时用作数组和映射。这在内部造成了一些不幸的后果。在内部,每个 Lua table 都有两部分:数组和哈希映射。
长度运算符只对table的数组部分进行操作;没有额外的内存用于存储 table 中的项目总数,包括哈希映射部分。如果需要该功能,则必须手动实施。一些好的方法是使用 getter 和 setter,手动更新本地计数器,或者使用代理 table 和 index 和 newindex 元方法。
作为一个有趣的旁注,有时很难判断一个值是存储在 table 的数组或散列部分中。考虑 Lua 5.3 中的这些示例:
1: t = {true, nil, true} -- #t = 3
2: t = {true, [2] = true} -- #t = 2
3: t = {true, [3] = true} -- #t = 1
4: t = {true, true, true} t[2] = nil -- #t = 3
它是零,因为你的 table a
不是一个序列。
序列是 table,它使用 1..n 中的键,其中 n 是序列的大小。
换句话说,#
用于序列长度,而不是table长度。
A table with exactly one border is called a sequence. For instance, the table
{10, 20, 30, 40, 50} is a sequence, as it has only one border (5). The table
{10, 20, 30, nil, 50} has two borders (3 and 5), and therefore it is not a
sequence. The table {nil, 20, 30, nil, nil, 60, nil} has three borders (0, 3,
and 6), so it is not a sequence, too. The table {} is a sequence with border 0.
Note that non-natural keys do not interfere with whether a table is a sequence.
When t is a sequence, #t returns its only border, which corresponds to the
intuitive notion of the length of the sequence. When t is not a sequence, #t
can return any of its borders. (The exact one depends on details of the
internal representation of the table, which in turn can depend on how the table
was populated and the memory addresses of its non-numeric keys.)
伙计们。有人可以帮我解决这个问题吗?
输入
a = {}
a.c = {1,2,3}
print(#a)
print(a.c)
输出
0
table: 0x11ed7a0
为什么#a 是 0?为什么不是 1?
谢谢。
Lua table 与其他语言中的结构不同。如 Lua manual 所示:
Tables are the main (in fact, the only) data structuring mechanism in Lua, and a powerful one. We use tables to represent ordinary arrays, symbol tables, sets, records, queues, and other data structures, in a simple, uniform, and efficient way.
此外,table 具有足够的动态性,您可以同时以多种方式使用它。例如,table 可以同时用作数组和映射。这在内部造成了一些不幸的后果。在内部,每个 Lua table 都有两部分:数组和哈希映射。
长度运算符只对table的数组部分进行操作;没有额外的内存用于存储 table 中的项目总数,包括哈希映射部分。如果需要该功能,则必须手动实施。一些好的方法是使用 getter 和 setter,手动更新本地计数器,或者使用代理 table 和 index 和 newindex 元方法。
作为一个有趣的旁注,有时很难判断一个值是存储在 table 的数组或散列部分中。考虑 Lua 5.3 中的这些示例:
1: t = {true, nil, true} -- #t = 3
2: t = {true, [2] = true} -- #t = 2
3: t = {true, [3] = true} -- #t = 1
4: t = {true, true, true} t[2] = nil -- #t = 3
它是零,因为你的 table a
不是一个序列。
序列是 table,它使用 1..n 中的键,其中 n 是序列的大小。
换句话说,#
用于序列长度,而不是table长度。
A table with exactly one border is called a sequence. For instance, the table {10, 20, 30, 40, 50} is a sequence, as it has only one border (5). The table {10, 20, 30, nil, 50} has two borders (3 and 5), and therefore it is not a sequence. The table {nil, 20, 30, nil, nil, 60, nil} has three borders (0, 3, and 6), so it is not a sequence, too. The table {} is a sequence with border 0. Note that non-natural keys do not interfere with whether a table is a sequence.
When t is a sequence, #t returns its only border, which corresponds to the intuitive notion of the length of the sequence. When t is not a sequence, #t can return any of its borders. (The exact one depends on details of the internal representation of the table, which in turn can depend on how the table was populated and the memory addresses of its non-numeric keys.)