【发布时间】:2021-07-01 13:45:58
【问题描述】:
--tables for the 1st example
local table1EX1={1,2,3}
local table2EX1={4,5}
local temp={}
-- 1st example:
temp=table1EX1
for i=1,2 do
temp[3+i]=table2EX1[i]
print("#temp = "..#temp)
end
-- output from the print command:
-- #temp = 4
-- #temp = 5
-- this is what I intended to do but I need it to adapt to a changing table size
--tables for the 2nd example
local table1EX2={1,2,3}
local table2EX2={4,5}
local temp={}
-- 2nd example:
temp=table1EX2
for i=1,#table2EX2 do
temp[#table1EX2+i]=table2EX2[i]
print("#temp = "..#temp)
end
-- output from the print command:
-- #temp = 4
-- #temp = 6
-- this is what I tried and where I noticed something is wrong
#table_name 只返回一个值,即命名表中从第一个条目到最后一个条目的数值范围。
确认这一点的测试: #1:
local tab={-1,0,_,nil,"a",nil,"x",_,3}
print(#tab) -- returns 9
#2:
local tab={1,2,3}
print(#tab) -- returns 3
tab[2]=nil
print(#tab) -- returns 3
#3:
local tab={1,2,3}
print(#tab) -- returns 3
tab[3]=nil
print(#tab) -- returns 2
我不明白在 example#1 和 example#2 之间出了什么问题以及为什么:代码应该工作相同:table1EX1 和 table1EX2 永远不会收到对其内容或大小的任何更改,因为 temp 变量用于存储两种情况下的值。然而在 example#2 中,代码突然导致 table1 的内容发生变化,从 print 命令输出可以看出。
虽然可以通过分配变量来避免整个问题,例如...
--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
...我想了解问题所在以及原因。 在这种情况下,英文中正确调用的“#”是什么?夏普,就像在 C# 中一样?菱形,就像字典上显示的那样?
【问题讨论】:
-
#长度操作符没有为非序列表定义,它应该只在你知道你的表是一个序列时使用,否则结果将是不可预测的。至于我们所说的英文#,它有很多名字number sign、pound、hashtag。我倾向于支持数字符号,或者如果特别是 lua,我会称之为长度运算符。 -
Lua 中# 操作符的规则很简单,据我所知:它告诉 如果 是一个序列 的表的长度是一个序列没有任何零值。强烈建议不要在任何其他情况下使用。
-
(它也明确指出,如果序列中有一个 nil,那么它可以返回任何数字,使得下一个为 nil。请不要在序列中包含 nil!)跨度>
标签: lua