【发布时间】:2014-06-28 17:52:01
【问题描述】:
Lua 有 # 运算符来计算用作数组的表的“长度”。 我检查了这个运算符,我很惊讶。
这是我在 Lua 5.2.3 下运行的代码:
t = {};
t[0] = 1;
t[1] = 2;
print(#t); -- 1 aha lua counts from one
t[2] = 3;
print(#t); -- 2 tree values, but only two are count
t[4] = 3;
print(#t); -- 4 but 3 is mssing?
t[400] = 400;
t[401] = 401;
print(#t); -- still 4, now I am confused?
t2 = {10, 20, nil, 40}
print(#t2); -- 4 but documentations says this is not a sequence?
谁能解释一下规则?
【问题讨论】:
-
解释规则是设计师的工作。他们在documentation 中这样做了。 (并非所有文档都对学习有用,但学习应该从这里开始并继续。)
标签: lua