Lua 语言
# Lua是一种轻量、小巧的脚本语言,用标准C语言编写并以源码形式开发。设计的摸底是为了嵌入到其他应用程序中,从而为应用程序提供灵活的拓展和定制功能。 # Lua安装 # 官网:https://www.lua.org # 1.官网下载源码 wget https://www.lua.org/ftp/lua-5.4.3.tar.gz # 2.编译安装 cd lua-5.4.3 make linux test make install # 3.如果test失败,需要安装 yum install -y readline-devel # 4.验证是否安装成功 lua -v # lua的语法和C/c++语法非常相似 # 交互式启动lua。直接输入lua命令就进入了lua的交互命令行 # 可以直接输入print('hello world') # 脚本式。创建的文件必须以.lua为后缀 # 执行脚本:lua abc.lua # 或者你可以直接指定执行脚本 #! /usr/local/bin/lue # 以下语法都兼容 a=1 b=a+2 a=1; b=a+2; a=1;b=a+2; a=1 b=a+2 # 单行注释 --print('hello world') # 多行注释 --[[ print('hello world') --]] # 关键字: and break do else elseif end false for function if in local nil not or repeat return then true until while goto # 运算符 # 算数运算符: +加 -减 *乘 /除 %取余 ^乘冥 -负号 # 关系运算符 10==10 true 10~=10 false 不等于 20>10 true 20<10 false 20>=10 true 20<=10 false # 逻辑运算符 and 逻辑与 A and B or 逻辑或 A or B not 逻辑非 取反 A=false B=true A and B false A or B true # 其他运算符 .. 两点用来链接两个字符串:"hello ".."world" 输出:hello world # 井号是医院运算符,返回字符串或者表的长度:#"hello" 输出:5 # 全局变量&局部变量 # 在lua语言中,全局变量无需声明即可使用。在默认情况下,变量总是认为是全局的,如果为提前赋值,默认为nil。 # 你想声明局部变量需要用到local关键字 # 如: local a = 100; print(a) # lua数据类型 nil(空,无效值) boolean(布尔,true/false) number(数值) string(字符串) function(函数) table(表) thread(线程) userdata(用户数据) # 你可以直接用type去查看 print(type(nil)) :string print(type(true)) :boolean print(type(1*1*1.1)) :number print(type("hello world")) :string print(type(io.stdin)) :userdata print(type(print)) :function print(type(type)) :function print(type(type{})) :table print(type(type{type(x)})) :string # 多行字符串的定义 html = [[ <html> <title> </title> </html> ]] # table类型 # 使用例子: a = {} arr = {"TOM","JERRY"} :数组的访问arr[1],lou下标是从1开始 b = {} b['x'] = 1 b['y'] = 2 :字典的访问:b['y'] arr = {"TOM",x=10,"Jerry",y=20} # 如何获取上面的值 TOM:arr[1] 10:arr['x'] 或者 arr.x Jerry:arr[3] 20:arr['y'] 或者 arr.y # function类型 # 声明函数 function f(a,b) print(a,b) end # 调用: f() :nil nil f(2) :2 nil f(2,6) :2 6 f(2,6,8) :2,6 # 函数参数随便你怎么传 # 可变参数&返回值 function add(...) a,b,c=... return c,b,a end a,b,c=add(1,2,3) # if then elseif else控制语句 function testif(a) if a>0 then print("a是正数") end end function testif(a) if a>0 then print("a是正数") else print("a是负数") end end function testif(a) if a<0 then print("a是负数") elseif a==0 then print("a是0") elseif a>=1 and a<=10 then print("a是1到10之间") else print("else") end end # while循环 function testwhile() local i = 1 while i<=10 do print(i) i=i+1 end end # repeat循环 until条件满足才退出循环 function testrepeat() local i=10 repeat print(i) i=i-1 until i < 1 end # for循环 for i=1,100,10 do print(i) end arr = {"Tom", "jerry", "Rows"} for i,v in ipairs(arr) do print(i,v) end # 输出,i是索引,v是值: 1 Tom 2 jerry 3 Rows arr = {"Tom", x="test", "jerry", "Rows"} for i,v in pairs(arr) do print(i,v) end # 输出,i是索引,v是值: 1 Tom 2 jerry 3 Rows x test
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?