参考http://blog.lujun9972.win/blog/2018/06/17/lua%E4%B8%AD%E7%A5%9E%E5%A5%87%E7%9A%84table/index.html
https://blog.csdn.net/terry7/article/details/47857147
1.lua里面字典、数组,都是用table来实现
t={}
t[{}] = "table" -- key 可以是 table
t[1] = "int" -- key 可以是整数
t[1.1] = "double" -- key 可以是小数
t[function () end] = "function" -- key 可以是函数
t[true] = "Boolean" -- key 可以是布尔值
t["abc"] = "String" -- key 可以是字符串
t[io.stdout] = "userdata" -- key 可以是userdata
t[coroutine.create(function () end)] = "Thread" -- key可以是thread
当作为字典来遍历时候,可以使用pairs来遍历,但是需要注意
使用 pairs 进行遍历时的顺序是随机的
具体原因暂时没时间看了
https://blog.csdn.net/weixin_42973416/article/details/103294010
for k,v in pairs(t) do
print(k,"->",v)
end
作为数组访问
for i,v in ipairs(a) do
print(i,v)
end
local my_labe={}
table.insert(my_table,'a')
my_table.remove(1) --1:index
删除key没有现成接口
需要通过字典长度来做
-- 删除table中的元素
local function removeElementByKey(tbl,key)
--新建一个临时的table
local tmp ={}
--把每个key做一个下标,保存到临时的table中,转换成{1=a,2=c,3=b}
--组成一个有顺序的table,才能在while循环准备时使用#table
for i in pairs(tbl) do
table.insert(tmp,i)
end
local newTbl = {}
--使用while循环剔除不需要的元素
local i = 1
while i <= #tmp do
local val = tmp [i]
if val == key then
--如果是需要剔除则remove
table.remove(tmp,i)
else
--如果不是剔除,放入新的tabl中
newTbl[val] = tbl[val]
i = i + 1
end
end
return newTbl
end
返回index和value。lua数组是从1下标开始
2.lua变量作用域有两种一种是全局变量,一种是局部变量
local x=1
y=2
装饰器,用来记录模块运行时间
local function dec(func,process)
local function _dec_in(...)
local input = { ... }
local t1 = aux.basic.now_ms()
local temp=func( ... )
local t2 = aux.basic.now_ms()
print(process,'use time',t2-t1,'ms')
return temp
end
return _dec_in
end
type()
定义函数
local function my_function()
end
通过require导入包