当前位置:网站首页>lua杂记
lua杂记
2022-08-11 05:31:00 【星际行走】
用传入索引拼接的key值,从table中获取对应的value
self.cardBaseCfg["attr"..index]
传入的index不同,可以获取不同的配置表属性。如果index为1,即相当于self.cardBaseCfg.attr1
判断table是否为空的正确做法
利用next函数
if next(a) ~= nil
--不是空表
else
--空表
end
如果a是数组,那么#a返回的是数组的长度,直接判断#a == 0就可以了。这个的前提是:a必须是数组才能这样做
链接:lua 判断table是否为空的正确做法_斧冰-CSDN博客_lua 判断table是否为空
Lua table中安全移除元素的方法
从后往前删除
for i = #test, 1, -1 do
if remove[test[i]] then
table.remove(test, i)
end
end
获取 table 的长度
list类型的,直接用#就行。其他的table无论是使用 # 还是 table.getn 其都会在索引中断的地方停止计数,而导致无法正确取得 table 的长度。
可以使用以下方法来代替:
function table_leng(t)
local leng=0
for k, v in pairs(t) do
leng=leng+1
end
return leng;
end
删除Table元素的两种方法
1、将字段赋值为 nil,适用于字典
2、使用Table库里的 table.remove(table, index),使用于列表
边栏推荐
- Some formulas for system performance and concurrency
- PyQt5中调用.ui转换的.py文件代码解释
- gerrit configure SSH Key and account, email information
- 解决AttributeError: ‘NoneType‘ object has no attribute ‘val‘ if left.val!=right.val:Line 17 问题
- Lua 快速入门(四)——多脚本执行
- helm安装
- Day 85
- JS case exercise (classic case of teacher pink)
- vim 编辑器使用学习
- The whole process of Tinker access --- configuration
猜你喜欢
随机推荐
【LeetCode-36】有效的数独
OpenMLDB Pulsar Connector: Efficiently connect real-time data to feature engineering
OpenGL 摄像机(Camera)类的创建
gerrit configure SSH Key and account, email information
Unity两种VR环境配置方法
C# 基础之字典——Dictionary(一)
three.js基础学习
欧拉法解微分方程
PAT乙级刷题之路
jdbc接口文档参考,jdbc接口方法逻辑探究
星盟-pwn-babyheap
Day 69
C语言实现三子棋(代码详解)
Day 72
JS case exercise (classic case of teacher pink)
【LeetCode-350】两个数组的交集II
helm安装
Building a data ecology for feature engineering - Embrace the open source ecology, OpenMLDB fully opens up the MLOps ecological tool chain
Matplotlib找不到字体,打印乱码
Unity 使用双缓冲实现一个好用的计时器