引子

环境 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. 关于ubuntu下qt编译显示Cannot connect creator comm socket /tmp/qt_temp.xxx/stub-socket的解决的方法

    今天在ubuntu下安装了qtcreator,准备測试一下能否用.果然一測试就出问题了,简单编写后F5编译在gnome-terminal中出现 Cannot connect creator comm ...

  2. uva--562Dividing coins +dp

    题意: 给定一堆硬币,然后将他们分成两部分,使得两部分的差值最小;输出这个最小的差值. 思路: 想了好久都没想到一个合适的状态转移方程.后面看了别人的题解后,才知道能够转成背包问题求解. 我们将全部的 ...

  3. 彻底删除"提示删除文件和目录"时出错的文件或目录

    当删除文件是出现上图时  能够用以下的方法删除文件 策略一:系统大法 第一招  进程帮你搞定 很多时候乱码文件名称里的文件是explorer.exe进程联系在一起的. 假设要删除的话能够这样:首先命令 ...

  4. 为了树莓派IIraspberrypi安装emacs+ecb+cedet+session+color-theme+cscope+linum

    类似这篇文章写的不多,为了避免以后大家转来转去而忽略了写文章的时间,这些特别加上是2014年6月28日,省的对不上一些软件的版本号(下文中有些"最新"的说法就相应这个时间).假设转 ...

  5. Struts工作机制图+OGNL+EL+值栈(Map,对象栈)

    struts 值栈  通过get set方法 方便的获取,设置属性值      比如从jsp页面传来的參数...从Action设置jsp所要回显的内容 注意EL表达式,struts2对request进 ...

  6. Spring MVC Hello World Example(转)

    Spring 3 You may interest at this Spring 3 MVC hello world example. In Spring MVC web application, i ...

  7. Android----------WindowManager

    我们Android平台是一个又一个的Activity组成的,每个Activity有一个或者多个View构成.所以说.当我们想显示一个界面的时候,我们首先想到的是建立一个Activity,然后全部的操作 ...

  8. oschina 手机/移动开发

    手机/移动开发 Android UI 组件(167) React Native 相关(8) 网站客户端(16) NativeScript 插件(18) iPhone/iPad开发工具(16) WP7开 ...

  9. php学习之道:php中soap的使用实例以及生成WSDL文件,提供自己主动生成WSDL文件的类库——SoapDiscovery.class.php类

    1. web service普及: Webservice soap wsdl差别之个人见解 Web Service实现业务诉求:  Web Service是真正"办事"的那个,提供 ...

  10. Net处理html页面元素工具类(HtmlAgilityPack.dll)的使用

    现在,在不少应用场合中都希望做到数据抓取,特别是基于网页部分的抓取.其实网页抓取的过程实际上是通过编程的方法,去抓取不同网站网页后,再进行 分析筛选的过程.比如,有的比较购物网站,会同时去抓取不同购物 ...