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]
- 参考
上一篇: lua学习记录