LUA:#表键引用突然改变了表项

LUA:#表键引用突然改变了表项,lua,Lua,#table_name只返回一个值,这是命名表中从第一个条目到最后一个条目的数值范围 确认这一点的测试: #1: #2: #3: 我不明白在示例1和示例2之间出现了什么问题以及为什么会出现这种情况:代码应该是一样的:table1EX1和table1EX2从未收到对其内容或大小的任何更改,因为temp变量用于存储这两种情况下的值。然而,在示例2中,代码突然导致表1的内容发生变化,这可以从print命令输出中看出 而整个问题可以通过指定变量来避免,比如 local tab={1,2,3} print

#table_name只返回一个值,这是命名表中从第一个条目到最后一个条目的数值范围

确认这一点的测试: #1:

#2:

#3:

我不明白在示例1和示例2之间出现了什么问题以及为什么会出现这种情况:代码应该是一样的:table1EX1和table1EX2从未收到对其内容或大小的任何更改,因为temp变量用于存储这两种情况下的值。然而,在示例2中,代码突然导致表1的内容发生变化,这可以从print命令输出中看出

而整个问题可以通过指定变量来避免,比如

local tab={1,2,3}
print(#tab) -- returns 3
tab[3]=nil
print(#tab) -- returns 2
…我想了解出了什么问题以及原因。
在这种情况下,英语中正确的“#”叫什么?夏普,像C#?菱形糖,就像字典上说的那样?

让我们看看你在这里做什么。见评论

例1 而不是

local table1EX1={1,2,3} -- create a table with 3 elements
local table2EX1={4,5} -- create another table with 3 elements
local temp={} -- create an empty table

temp=table1EX1 -- temp now refers to table1EX1
-- in a loop append the 2 elements of table2EX1 to table2EX1 (or temp, same table)
for i=1,2 do
  temp[3+i]=table2EX1[i] -- first run temp[3+1], second temp[3+2]
  print("#temp = "..#temp)
end
简单地写

local temp = {}
temp = table1EX1
例2 您向
temp
添加了1个元素,因此向
table1x2
添加了1个元素,因为两者都引用同一个表。因此,在第二个循环循环中,表1EX2是
4
。这就是与示例1的区别

通过引用复制表值
local temp=table1EX1
不会将任何值从
table1EX1
复制到
temp
temp
只是对表
table1x1
的另一个引用

local table1EX2={1,2,3} -- create a table with 3 elements
local table2EX2={4,5}  -- create a table with 2 elements
local temp={}  -- create an empty table

temp=table1EX2  -- temp now refers to table1EX2
-- in a loop from 1 to #table2EX2 (2)
for i=1,#table2EX2 do
  temp[#table1EX2+i]=table2EX2[i] -- first run temp[3+1], second temp[4+2]
  print("#temp = "..#temp)
end
这是危险的。请参阅Lua手册:

当t是一个序列时,#t返回其唯一的边框,对应于 序列长度的直观概念。当t不是一个 序列,#t可以返回其任何边界。(确切数字取决于 表的内部表示形式的详细信息,这反过来又可以 取决于表的填充方式及其内存地址 非数字键。)

仅当您知道t是序列时才使用长度运算符。这是一个Lua表,整数索引为1,…n,没有任何间隙

在这种情况下,英语中正确的“#”叫什么?锋利的, 就像在C中一样#

这是数字符号或散列。对于Lua,它被称为长度操作符


阅读此

这是否回答了您的问题?未为非序列表定义
#
长度运算符,仅当您知道表是序列时才应使用该运算符,否则结果将不可预测。至于我们在英语中所称的
,它有许多名称
数字符号
英镑
标签
。我倾向于使用数字符号,或者如果特别是lua,我会称之为长度运算符。就我所知,lua中#运算符的规则很简单:如果没有任何nil值,它会告诉一个表的长度,这是一个序列。非常不鼓励在任何其他情况下使用。(它还指定,如果序列中有nil,那么它可能返回任何数字,以便下一个为nil。请不要在序列中使用nil!)谢谢。另一件事是学到的。
--tables for the 2nd example
local table1EX2={1,2,3}
local table2EX2={4,5}
local temp={}

-- 2nd example:
temp=table1EX2
local x,y=#table2EX2,#table1EX2
for i=1,x do
  temp[y+i]=table2EX2[i]
  print("#temp = "..#temp)
end
local table1EX1={1,2,3} -- create a table with 3 elements
local table2EX1={4,5} -- create another table with 3 elements
local temp={} -- create an empty table

temp=table1EX1 -- temp now refers to table1EX1
-- in a loop append the 2 elements of table2EX1 to table2EX1 (or temp, same table)
for i=1,2 do
  temp[3+i]=table2EX1[i] -- first run temp[3+1], second temp[3+2]
  print("#temp = "..#temp)
end
local temp = {}
temp = table1EX1
local temp = table1EX1
local table1EX2={1,2,3} -- create a table with 3 elements
local table2EX2={4,5}  -- create a table with 2 elements
local temp={}  -- create an empty table

temp=table1EX2  -- temp now refers to table1EX2
-- in a loop from 1 to #table2EX2 (2)
for i=1,#table2EX2 do
  temp[#table1EX2+i]=table2EX2[i] -- first run temp[3+1], second temp[4+2]
  print("#temp = "..#temp)
end
local tab={-1,0,_,nil,"a",nil,"x",_,3}
print(#tab) -- returns 9