首页 > 代码库 > lua5.1 和 5.2 关于 sequence 的定义变化,对#table取值的影响
lua5.1 和 5.2 关于 sequence 的定义变化,对#table取值的影响
引子
环境 lua 5.2
a = {}
for i=1,2 do a[i] = i*3 end
a[4] = 11;
print(a[#a])
---print 11
-----------------------------------
a = {}
for i=1,3 do a[i] = i*3 end
a[5] = 11;
print(a[#a])
----print 9
---------------------------------
#a 第一个的值是4 第二个的值是3
根据我之前从书中看到的知识点来看的话 #a 第一个的值是2 第二个的值是3 才对啊
为啥呢
刚开始入门,源代码部分就跳过吧,又没有合适的关键词搜索,只能到大婶群里问了
一会儿 大神来了 给了个牛逼的解释
大婶:
5.1 和 5.2 关于 sequence 的定义有变化.
5.1 的
2.5.5 – The Length Operator
The length operator is denoted by the unary operator #. The length of a string is its number of bytes (that is,
2.5.5 – The Length Operator
The length operator is denoted by the unary operator #. The length of a string is its number of bytes (that is,
the usual meaning of string length when each character is one byte).
The length of a table t is defined to be any integer index n such that t[n] is not nil and t[n+1] is nil;
The length of a table t is defined to be any integer index n such that t[n] is not nil and t[n+1] is nil;
moreover, if t[1] is nil, n can be zero. For a regular array, with non-nil values from 1 to a given n,
its length is exactly that n, the index of its last value. If the array has "holes" (that is, nil values between other non-nil values), then #t can be any of the indices that directly precedes a nil value (that is,
it may consider any such nil value as the end of the array).
5.2 的
3.4.6 – The Length Operator
The length operator is denoted by the unary prefix operator #. The length of a string is its number of bytes (that is,
3.4.6 – The Length Operator
The length operator is denoted by the unary prefix operator #. The length of a string is its number of bytes (that is,
the usual meaning of string length when each character is one byte).
A program can modify the behavior of the length operator for any value but strings through the __len metamethod
A program can modify the behavior of the length operator for any value but strings through the __len metamethod
(see §2.4).
Unless a __len metamethod is given, the length of a table t is only defined if the table is a sequence, that is,
Unless a __len metamethod is given, the length of a table t is only defined if the table is a sequence, that is,
the set of its positive numeric keys is equal to {1..n} for some integer n. In that case, n is its length. Note that a table like
{10, 20, nil, 40}
is not a sequence, because it has the key 4 but does not have the key 3.
{10, 20, nil, 40}
is not a sequence, because it has the key 4 but does not have the key 3.
(So, there is no n such that the set {1..n} is equal to the set of positive numeric keys of that table.) Note, however, that non-numeric keys do not interfere with whether a table is a sequence.
从语言角度讲, lua 5.1 定义了 # 对数组取长度的约束. 而 lua 5.2 不严格定义了,只说如果有 nil 就不确定
也就是说 lua 5.2 放宽了语言定义,允许实现更灵活
从语言角度讲, lua 5.1 定义了 # 对数组取长度的约束. 而 lua 5.2 不严格定义了,只说如果有 nil 就不确定
也就是说 lua 5.2 放宽了语言定义,允许实现更灵活
我:
很高深啊 我得慢慢啃
--[[高潮部分,直达G点]]
很高深啊 我得慢慢啃
--[[高潮部分,直达G点]]
大婶:
没什么高深的, lua 5.2 说,你有 nil 就别指望 #table 对
没什么高深的, lua 5.2 说,你有 nil 就别指望 #table 对
dun悟了......................level +1
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。