引子

环境 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. 使用svnkit 的相关实例及相关问题汇总

    SVNKIT操作SVN版本库的完整例子 http://www.cnblogs.com/wangjiyuan/p/svnkitwanchenglizi.html#!comments 2.SVNClien ...

  2. ACM-最小生成树之畅通project——hdu1863

    ***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...

  3. 表空间的状态(二) - read/write

    表空间状态-READ ONLY.READ WRITE 1. 仅仅读表空间的主要用途就是为了消除对数据库大部分静态数据的备份和恢复的须要.Oracle不会更新仅仅读表空间爱你的文件.因此这部分文件能够存 ...

  4. 初窥Linux 之 区分硬连接和软连接

    一.Linux下的两种连接文件及创建方式 在Linux下面的连接文件有两种——软连接和硬连接,虽然都是连接文件,但两者却有很大的区别.一种是类似于Windows的快捷方式功能的文件(或目录),这种连接 ...

  5. JAVA中enum的常见用法

    JAVA中enum的常见用法包括:定义并添加方法.switch.遍历.EnumSet.EnumMap 1.定义enum并添加或覆盖方法 public Interface Behaviour{ void ...

  6. [Android学习笔记]View的measure过程学习

    View从创建到显示到屏幕需要经历几个过程: measure -> layout -> draw measure过程:计算view所占屏幕大小layout过程:设置view在屏幕的位置dr ...

  7. 移植ffmpeg到VC环境心得

    所有想学习ffmpeg的网友有福了,大名鼎鼎的ffmpeg,移植到Windows的VC6版本全部开源,编译环境为VC6+SP5+VCPP5.别忘记了顶贴哦. 移植ffmpeg到windows,主要的修 ...

  8. latex命令替换之\newcommand

    有时候我们在用latex写文档的时候不想写很长的命令,那么我们自己定义一个新的命令来替换一段代码. 举例如下: \usepackage{booktabs} \usepackage{multirow} ...

  9. STM32M CUBE实现printf打印调试信息以及实现单字节接收

    在写单片机程序时我们一般喜欢使用printf来通过串口打印调试信息,但这个函数是不能够直接使用的.必须做点对库函数的修改. 具体project下载地址: http://download.csdn.ne ...

  10. MVC Json 回报

    /// <summary> /// 获取评论列表 /// </summary> /// <param name="pageIndex">< ...