引子

环境 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. linux-sfdisk 使用方法

    功能说明:硬盘分区工具程序. 语 法:sfdisk [-?Tvx][-d <硬盘>][-g <硬盘>][-l <硬盘>][-s <分区>][-V < ...

  2. 虚幻4随笔4 从project開始

     前文说到UE3開始.虚幻就使用了UnrealBuildTool(下面简称UBT)来编译和生成代码. 为什么这么做而不是使用VS是非常好理解的:由于VS跨平台会比較麻烦.像虚幻这样体量的proje ...

  3. Linux Shell 函数返回值

    Shell函数返回值,常用的两种方式:return,echo 1) return 语句 shell函数的返回值,可以和其他语言的返回值一样,通过return语句返回. 示例: #!/bin/sh fu ...

  4. Eclipse 每次打开workspace目录记录位置?

    E:\eclipse_j2ee eclipse安装根目录 在这个文件下: E:\eclipse_j2ee\configuration\.settings\org.eclipse.ui.ide.pref ...

  5. 关于matlab矩阵卷积conv2和傅里叶变换求卷积ifft2的关系

    先定义两个矩阵 a = [1 2 3 5 ; 4 7 9 5;1 4 6 7;5 4 3 7;8 7 5 1] %a矩阵取5*4 b = [1 5 4; 3 6 8; 1 5 7]   %b矩阵如多数 ...

  6. Indiegogo: An International Crowdfunding Platform to Raise Money

    Indiegogo: An International Crowdfunding Platform to Raise Money The world's funding platform. Fund ...

  7. hdu 4740【模拟+深搜】.cpp

    题意: 给出老虎的起始点.方向和驴的起始点.方向.. 规定老虎和驴都不会走自己走过的方格,并且当没路走的时候,驴会右转,老虎会左转.. 当转了一次还没路走就会停下来.. 问他们有没有可能在某一格相遇. ...

  8. SE 2014年5月6日

    如图配置: 三台交换机两两相连接,构成一二层环路,同时为了保证链路的较为可靠,使用双线链接 请用自己的语言描述以上拓扑搭建的优劣势:并使用哪些技术较为合理,请描述并实施 SW3为接入层交换机,下链接三 ...

  9. poj3468(线段树)

    题目连接:http://poj.org/problem?id=3468 线段树功能:update:成段增减 query:区间求和. 分析:需要用到延迟标记(或者说懒惰标记),简单来说就是每次更新的时候 ...

  10. 祖国版Solowheel!IPS103 独轮思维车 - 三个月体验报告

    http://tieba.baidu.com/f?kz=2308652773&mo_device=1