zoukankan      html  css  js  c++  java
  • LUA 利用#遍历表的问题

     1 tb ={ '0','1',2}
     2 t = {
     3     "hello",
     4     1,
     5     2,
     6     'w',
     7     4,
     8     tb
     9 }
    10 
    11 --~ 1    hello
    12 --~ 2    1
    13 --~ 3    2
    14 --~ 4    w
    15 --~ 5    4
    16 --~ 6    table: 001FB1D0
    17 for i=1, #t do
    18     print(i, t[i])
    19 end
    20 
    21 --~ 1    hello
    22 --~ 2    1
    23 --~ 3    2
    24 --~ 4    w
    25 --~ 5    4
    26 --~ 6    table: 001FB1D0
    27 for k, v in pairs(t) do
    28     print(k, v)
    29 end
    30 
    31 t1 = {
    32     "hello",
    33     [1] = 72, --错误 的方式,"hello"的key已经是[1]了,此处无效
    34     [2] = 105,--错误
    35     'w',
    36     [3] = 98,--错误
    37     tb
    38 }
    39 
    40 --~ 1    hello
    41 --~ 2    w
    42 --~ 3    table: 001FB1D0
    43 for i=1, #t1 do
    44     print(i, t1[i])
    45 end
    46 
    47 --~ 1    hello
    48 --~ 2    w
    49 --~ 3    table: 001FB1D0
    50 for k, v in pairs(t1) do--
    51     print(k, v)
    52 end
    53 
    54 
    55 
    56 t2 = {
    57     "hello",
    58     [10] = 72,
    59     [20] = 105,
    60     'w',
    61     [30] = 98,
    62     tb
    63 }
    64 print("--------------------")
    65 --~ 1    hello
    66 --~ 2    w
    67 --~ 3    table: 002CB1D0
    68 for i=1, #t2 do --#遇到不连续key中断,因此只遍历到[1],[2],[3]
    69     print(i, t2[i])
    70 end
  • 相关阅读:
    二维空间轨迹聚类
    Java 格式化输出
    Linux 小笔记
    Nginx ServerName 配置说明(转)
    LVS
    Nginx + tomcat 实现简单集群(基于反向代理方式)
    mysql 外键
    tomcat 日志输出
    tomcat https
    SpringMVC表单标签简介
  • 原文地址:https://www.cnblogs.com/timeObjserver/p/6226291.html
Copyright © 2011-2022 走看看