当前位置: 首页 > news >正文

Lua现学现卖

一、Lua的变量类型

        全局变量:MyVar

        局部变量:local MyVar

二、Lua的数据类型

        1.nil:一个空值 类似C++的nullptr

        2.Boolean:true/false 类似C++的bool

        3.string:字符串 类似C++的std::string

        4.Number:数字 类似C++的double 在Lua中值都是双精度没有int

        5.table:一个表存放数据,可以是不同类型的数据

三、运算符

        1.+ - * /

        2.== :等于   ~= :不等于  < 小于  >大于  <=小于等于  >=大于等于

        3. and并且  or 或者

四、控制结构 

(while)
index = 1
while index < 10 doprint(index)index = index + 1
end(repeat)
index = 1
repeatprint(index)index = index + 1
until index > 10(if)
a = 5
b = 10
if (a < b) thenprintf("a < b")
end(for)
--这个for循环默认的增加量是1
for index = 1 , 10 doprintf(index)
end--这个for循环每次的增加量是2 开始时是1第二次是1+2第三次是1+2+2
for index = 1 , 10 , 2 doprint(index)
end(break)
for index = 1, 100 doif index == 52 thenprint("index == 52")breakend
end

五、函数

function FunctionName(par1,par2,...)print("函数体")
end
在Lua的函数中可以没有参数使用...替代
function FunctionName(...)--可以通过arg.n来获得变量的个数 这个arg是一个table表用来存储所有的变量 Lua自己生成的表不用手动生成print("")
end

六、字符串

--tonumber将字符串转换成数字
myString = "1234"
myNumber = tonumber(myString)--tostring将数字转换成字符串
myNumber = 1234
myString = tostring(myNumber)--string.len获得字符串的长度
myString = "1234"
print(string.len(myString)) --4--string.sub(myString , start , end)
myString = "Hello World"
newString = string.sub(myString , 1 , 5)
print(newString) -- Hello

 七、表Table

-- 假设
-- 创建一个表添加100个元素
myTable = {}
for index = 1 , 100 domyTable[index] = math.random(1,1000)
end-- table.getn(myTable) 获得表的大小
print(table.getn(myTable))--table.insert(myTable,position,value) 在表中的指定位置插入一个值
table.insert(myTable,20,"Hello World") -- 在表的第25位置处插入字符串"Hello World"--table.ramove(myTable,position) 移除表指定位置的元素
print(table.ramove(myTable,20))-- pairs 可以用于遍历table中的每个元素
方式一、for index , value in pairs(myTable) doprint(index,value)    end
方式二、for index , table.getn(myTable) doprint(index,myTable[index])end

相关文章:

  • SpringBoot项目使用arthas-tunnel-server
  • AtCoder AT_abc412_c [ABC412C] Giant Domino 题解
  • 【力扣 简单 C】121. 买卖股票的最佳时机
  • GitHub Actions 实现 AWS ECS 服务的多集群安全重启方案
  • 【AI实践】Mac一天熟悉AI模型智能体应用(百炼版)
  • STM32中Usart的使用
  • 一个简单测试Deepseek吞吐量的脚本,国内环境可跑
  • 1.1 基于Icarus Verilog、ModelSim和Vivado对蜂鸟E203处理器进行仿真
  • HarmonyOS File和base64字符串转换
  • Note2.2 机器学习训练技巧:Batch and Momentum(Machine Learning by Hung-yi Lee)
  • C语言二级指针与多级指针
  • cannot import name ‘TextKwargs‘ from ‘transformers.processing_utils‘
  • 【LeetCode 热题 100】438. 找到字符串中所有字母异位词——(解法二)定长滑动窗口+数组
  • LeetCode Hot 100 找到字符串中所有字母异位词
  • 编译流程详解
  • 利用ROS打印novatel_msgs/INSPVAX
  • 滑坡监测接收机市场分析
  • libxlsxwriter: 一个轻量级的跨平台的C++操作Excel的开源库
  • 个人日记本小程序开发方案(使用IntelliJ IDEA)
  • python解释器 与 pip脚本常遇到的问题汇总