引子

环境 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, 
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; 
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,
 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 
(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,
 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.
 (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 放宽了语言定义,同意实现更灵活

 
我:

非常高深啊   我得慢慢啃  





--[[高潮部分,直达G点]]
大婶:

没什么高深的, lua 5.2 说,你有 nil 就别指望 #table 对




dun悟了......................level   +1

lua5.1 和 5.2 关于 sequence 的定义变化,对#table取值的影响的更多相关文章

  1. hdu 2817 A sequence of numbers(快速幂取余)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2817 题目大意:给出三个数,来判断是等差还是等比数列,再输入一个n,来计算第n个数的值. #inclu ...

  2. 杭电 2817 A sequence of numbers【快速幂取模】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2817 解题思路:arithmetic or geometric sequences 是等差数列和等比数 ...

  3. Hibernate(4)——主键生成策略、CRUD 基础API区别的总结 和 注解的使用

    俗话说,自己写的代码,6个月后也是别人的代码……复习!复习!复习!涉及的知识点总结如下: hibernate的主键生成策略 UUID 配置的补充:hbm2ddl.auto属性用法 注解还是配置文件 h ...

  4. hibernate注解方法使用总结(转)

    原博文地址:http://blog.sina.com.cn/s/blog_935ebb670101dnre.html 1.类级别注解 @Entity     映射实体类 @Table    映射数据库 ...

  5. Hibernate注解配置

    在之前的第一次对框架的实际应用中,我使用的是Hibernate的xml配置方法,xml配置方法非常繁琐, 还是推荐所有使用Hibernate的人使用注解方式进行配置,在这篇文章中,我将列举出我们常用的 ...

  6. TCP中的RST复位信号

    TCP中的RST复位信号 在TCP协议中RST表示复位,用来关闭异常的连接,在TCP的设计中它是不可或缺的. 发送RST包关闭连接时,不必等缓冲区的包都发出去,直接就丢弃缓存区的包发送RST包.而接收 ...

  7. Hibernate 注解的用法以及说明

    1.类级别注解 @Entity     映射实体类 @Table    映射数句库表  @Entity(name="tableName") - 必须,注解将一个类声明为一个实体be ...

  8. ORACLE PL/SQL编程详解

    ORACLE PL/SQL编程详解 编程详解 SQL语言只是访问.操作数据库的语言,并不是一种具有流程控制的程序设计语言,而只有程序设计语言才能用于应用软件的开发.PL /SQL是一种高级数据库程序设 ...

  9. Hibernate注解方法使用总结

    1.类级别注解 @Entity     映射实体类 @Table    映射数句库表 @Entity(name="tableName") - 必须,注解将一个类声明为一个实体bea ...

随机推荐

  1. ubuntu14.04中virtualbox虚拟机无法启动

    近期升级了ubuntu14.04,还是按之前的方法安装了virtualbox(guest系统仍然使用升级之前的镜像文件),可是在启动guest系统时,总是报错,提演示样例如以下: Kernel dri ...

  2. JavaScript 中的事件类型3(读书笔记思维导图)

    Web 浏览器中可能发生的事件有很多类型.如前所述,不同的事件类型具有不同的信息,而“ DOM3级事件”规定了以下几类事件. UI(User Interface,用户界面)事件:当用户与页面上的元素交 ...

  3. 关于JAVA Project.waitfor()返回值是1

    Project.waitfor()返回值是1,找了很久从网上没有发现关于1的说明. 这时对源代码调试了一下,发现Project=null.而去根目录下点击被调用的bat文件发现也可以被正确执行. 这时 ...

  4. JavaScript2谁刚开始学习应该知道4最佳实践文章(翻译)

    原版的:24 JavaScript Best Practices for Beginners (注:阅读原文的时候没有注意公布日期,觉得不错就翻译了,翻译到JSON.parse那一节觉得有点不正确路才 ...

  5. TCP/UDP简介

    TCP/UDP简介 Socket小白篇-附加TCP/UDP简介 Socket 网络通信的要素 TCP和UDP Socket的通信流程图 1.Socket 什么是Socket Socket:又称作是套接 ...

  6. s nrmtyu,yi.sfn rt

    http://www.zhihu.com/collection/24337307 http://www.zhihu.com/collection/24337259 http://www.zhihu.c ...

  7. 使用CXF创建REST WEBSERVICE

    简单小结下CXF跟REST搭配webservice的做法,直接举代码为样例: 1 order.java   package com.example.rest; import javax.xml.bin ...

  8. 通过YAJL生成json语句

    这里主要介绍的是怎样通过yajl生成一个json语句.方法通过代码就能够非常清楚的看到了,只是这里仅仅加入了字符串. 假设须要加入其它类型的,能够查考yajl的手冊,调用其它函数进行加入. /* * ...

  9. WindowsclientC/C++编程规范“建议”——前言

    前言 工作中接触了非常多编程规范.当中最有意思的是,公司近期公布了一版C/C++编程规范,然后我看到该规范的最后一段时,有这么一句:"该规范不适用于Windows平台开发".看来这 ...

  10. Hdu 4738【求无向图的桥】.cpp

    题目: 曹操在长江上建立了一些点,点之间有一些边连着.如果这些点构成的无向图变成了连通图,那么曹操就无敌了.刘备为了防止曹操变得无敌,就打算去摧毁连接曹操的点的桥.但是诸葛亮把所有炸弹都带走了,只留下 ...