Table.Intert不代替表

问题描述 投票:0回答:1

lua lua-table
1个回答
0
投票

,我们可以看到table.insert(t, 42)

行为
t[#t + 1] = 42

3.4.7 - 长度运算符:

在表上应用的长度运算符返回该表中的边框。表T中的边框是满足以下条件的任何非负整数: (border == 0 or t[border] ~= nil) and (t[border + 1] == nil or border == math.maxinteger)

[...]
一个完全一个

border的表称为

。
[...]
当t是一个序列时,#t返回其唯一的边框,这对应于序列长度的直观概念。

当t不是一个序列时,#t可以返回其任何边界。非数字键。) 换句话说,在没有指定键的情况下对它们呼叫
table.insert将插入由表的内部表示确定的索引中给出的值。 其他单词,在具有多个边界的表上调用table.insert
未指定位置的表,将在表格的内部表示确定的索引上插入值。 如果可移植性是一个问题,请避免这种情况。在我的系统上,示例的结果在Lua和Luajit的参考实现之间有所不同。
$ lua -v Lua 5.4.6 Copyright (C) 1994-2023 Lua.org, PUC-Rio $ luajit -v LuaJIT 2.1.1734355927 -- Copyright (C) 2005-2023 Mike Pall. https://luajit.org/ $ diff <(lua example.lua) <(luajit example.lua) 2c2 < t[2] = nil --- > t[2] = 10 4c4 < t[4] = 10 --- > t[4] = nil 17,19c17,19 < x[3] = nil < x[4] = 2 < x[5] = 10 --- > x[3] = 2 > x[4] = 10 > x[5] = nil


最新问题
© www.soinside.com 2019 - 2024. All rights reserved.