引子

环境 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.   PS2: 这篇文章中的图片绘图工具使用的是Dia (sudo apt-get install dia)。据说yEd也很不错。

    SBCL编译过程 - O.Nixie的专栏 - 博客频道 - CSDN.NET PS2: 这篇文章中的图片绘图工具使用的是Dia (sudo apt-get install dia).据说yEd也很不 ...

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

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

  3. [C++]C++中的运行时类型检测

    Date:2014-1-3 Summary: 使用C++中的运行时类型检测.(文章重点在于记录本人的使用情况,并非深层讨论RTTI) Contents:写习惯C#的我,在C++依然存在哪些.NET的惯 ...

  4. JEECG开源团队招募新成员 2014年

    JEECG开源团队招募新成员 2014年 截止日期:2014-06-01        JEECG开源项目 是一款基于代码生成器的微云高速开发平台.提供企业高速开发和採用微信实现移动应用的解决方式.J ...

  5. birdnest是什么意思_birdnest在线翻译_英语_读音_用法_例句_海词词典

    birdnest是什么意思_birdnest在线翻译_英语_读音_用法_例句_海词词典 birdnest

  6. [WPF]使用Pack URI路径訪问二进制资源

    一.路径格式定义 完整的URI定义为: pack://application,,,[/可选程序集名称;][可选版本;][目录名称/]文件名 缩略后的写法是: [目录名称/]文件名 二.在XAML代码中 ...

  7. nginx 301跳转到带www域名方法rewrite(转)

    首先一.得在你的域名管理里面定义 test.com和www.test.com指向你的主机ip地址,我们可以使用nslookup命令测试:直接输入 nslookup test.com和nslookup ...

  8. 【c语言】模拟库函数strstr

    // 模拟库函数strstr #include <stdio.h> #include <assert.h> const char* my_strstr(const char * ...

  9. Max Sum (hdu 1003 简单DP水过)

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  10. AngularJs 基础(60分钟入门) (转)

    AngularJs是一个不错的用于开发SPA应用(单页Web应用)的框架.单页Web应用(single page web application,SPA),就是只有一张Web页面的应用.浏览器一开始会 ...