13

主题

25

回帖

991

积分

优秀创作者

积分
991

夏日·闪闪发光勋章

发表于  2023-5-30 22:21:11 | 显示全部楼层 |阅读模式 6990 1
1.结论:lua5.4版本的#运算符,其结果为:表的一个随机边界index.

2.边界index:值为nil的元素的pre元素的index(0 <= 边界index <= math.maxinteger)
例_1:{1, 2, 3} --> [4] = nil,边界index为3
例_2:{1, 2, nil, 4, 5} --> [3] = nil,[6] = nil边界index为2和5
例_3:{nil, 1, 2} --> [1] = nil, [4] = nil,边界index为1或3
例_4:{2, 3, nil, 3, nil, 4} --> [3] = nil, [5] = nil, [7] = nil, 边界index为2, 4, 6
例_5:{[1] = 1, [2] = 2, [4] = 4, [5] = 5, [6] = 6, [7] = 7} --> [3] = nil,[8] = nil,边界index为2和7
例_6:{[1] = 1, [2] = 2, [3] = 4, [5] = 5, [6] = 6, [7] = 7} --> [4] = nil,[8] = nil,边界index为3和7
3.#table的时候会根据table的address取具体的边界index,所以上述例子输出结果:
  1. > t = {1, 2, 3}
  2. > #t
  3. 3
  4. > t = {1, 2, nil, 4, 5}
  5. > #t
  6. 5
  7. > t = {nil, 1, 2}
  8. > #t
  9. 3
  10. > t = {2, 3, nil, 3, nil, 4}
  11. > #t
  12. 6
  13. > t = {[1] = 1, [2] = 2, [4] = 4, [5] = 5, [6] = 6, [7] = 7}
  14. > #t
  15. 7
  16. > t = {[1] = 1, [2] = 2, [3] = 4, [5] = 5, [6] = 6, [7] = 7}
  17. > #t
  18. 3
复制代码


发表于  2023-5-31 10:19:48 | 显示全部楼层
太强了!可惜我不写lua了。。。哈哈哈