欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

lua 笔记

程序员文章站 2024-03-17 22:56:58
...

mark一下用到的lua的一些语法

lua的数据结构table
test = {"Tom", "Mary", "Jam","Hey"}
lua读取文件
local load_comeback_actor_from_file = function(file_name)
    local file = io.open(file_name)
    for l in file:lines() do
        local key = nil 
        for n in string.gmatch(l,"[+-]?[0-9]*[%.]?[0-9]+[e]?[+]?[-]?[0-9]*") do
            if key == nil then
                key = n 
                -- print("key > " .. key)
            else
                if actor_login_table[key] == nil then
                    actor_login_table[key] = {}
                end
                table.insert(actor_login_table[key],n)
            end
        end
    end
end

-- 打印table
local debug_print_table = function(tblName)
    for i, v in pairs(tblName)
    do
        local key = i
        local key_len = #v    -- 获得table的长度
        print("key > ",key) 
        for index=1, key_len, 1 do
            print(" value -> ", v[index])
        end
    end
end

文件的格式

1243274972130967574 1552233600 1552320000
执行之后的结果
key = 1243274972130967574 
value = {1552233600, 1552320000}
lua访问db工具中对应数组的下标的时候
index1 = 5
index2 = 5.0 

array[index1]  -- ok
array[index2]  -- not ok

-- 解决的办法
local new_index = math.floor(index2)
array[new_inde] 

  • 参考
  1. lua学习笔记 https://www.kancloud.cn/digest/luanote/119923