zoukankan      html  css  js  c++  java
  • LUA 删除元素的问题

    table在删除元素时要注意,例
    t = { "hello", "world", "!"}
    t[1] = nil
    此时print(#t) --输出3,就是说把表的元素置为nil并没有移除该表项。

    但,若是:

    t = {
    [1] = nil,
    [2] = 223,
    [3] = nil
    }
    
    function count(t)
        local c = 0
        for k, v in pairs(t) do
            c = c + 1
        end
        return c
    end
    
    print(count(t))--1

     再看更诡异的

     1 t = {
     2 [1] = 12,
     3 [2] = nil,
     4 [3] = 4
     5 }
     6 
     7 function count(t)
     8     local c = 0
     9     for k, v in pairs(t) do
    10         c = c + 1
    11     end
    12     return c
    13 end
    14 
    15 print("------------------", #t)
    16 t2 = { "hello", "world", "!"}
    17 t2[1] = nil
    18 t2[22] = nil
    19 t2[9] = nil
    20 print("--------t2---------", #t2)
    21 for k, v in pairs(t2) do
    22     print(k, v)
    23 end
    24 
    25 print("--------------->")
    26 for i=1, #t2 do
    27     print(i, t2[i])
    28 end
  • 相关阅读:
    EM算法
    最大熵模型中的对数似然函数的解释
    PySpark 自定义函数 UDF
    PySpark 自定义聚合函数 UDAF
    Numpy总结
    Examples
    Examples
    Examples
    Examples
    Examples
  • 原文地址:https://www.cnblogs.com/timeObjserver/p/6404361.html
Copyright © 2011-2022 走看看