当前位置:网站首页>Lua的基本使用
Lua的基本使用
2022-06-23 09:00:00 【爱锅巴】
从打印hello world开始
输入命令 lua -i 或 lua 来启用交互式编程模式,使用print函数打印字符串"hello world":
[[email protected] ~]# lua
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
> print("hello world")
hello world
>
有交互是编程,也有脚本式编程
编写脚本指定解释器为 /bash/lua (本人为centos7的系统,自带lua,如果其他版本没有Lua的需要自行安装)
编写脚本
[[email protected] ~]# vim hello.lua
脚本内容
#!/bin/lua
print("hello world")
执行脚本
[[email protected] ~]# lua hello.lua
hello world
赋权后直接执行赋权操作只需执行一次
[[email protected] ~]# chmod +x hello.lua
[[email protected] ~]# ./hello.lua
hello world
数据类型
Lua中有8个基本类型分别为:nil、boolean、number、string、userdata、function、thread和table
| 数据类型 | 描述 |
|---|---|
| nil | 这个最简单,只有值nil属于该类,表示一个无效值(在条件表达式中相当于false) |
| boolean | 包含两个值:false和true |
| number | 表示双精度类型的实浮点数 |
| string | 字符串由一对双引号或单引号来表示 |
| function | 由 C 或 Lua 编写的函数 |
| userdata | 表示任意存储在变量中的C数据结构 |
| thread | 表示执行的独立线路,用于执行协同程序 |
| table | Lua 中的表(table)其实是一个"关联数组"(associative arrays),数组的索引可以是数字或者是字符串。在 Lua 里,table 的创建是通过"构造表达式"来完成,最简单构造表达式是{},用来创建一个空表 |
type函数可以查看数据类型
[[email protected] ~]# lua
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
> print(type(nil))
nil
> print(type(true))
boolean
> print(type("hello world"))
string
> print(type(type))
function
> print(type(23))
number
通过例子快速了解其中几个数据类型和逻辑处理的语法
string
声明string变量“Tom”,此处是全局变量,只要不退出客户端界面,就可以一直使用这个变量
local声明string变量“Jerry”,这是个局部变量,只能在当前作用域使用
> Tom="I'm a cat"
> print(Tom)
I'm a cat > local Jerry="I'm a mouse" print(Jerry) --局部变量只能在当前作用域使用 I'm a mouse > print(Jerry) nil > local Jerry="I'm a mouse" print(type(Jerry)) --局部变量只能在当前作用域使用 string > print(type(Jerry)) nil > print(Tom) I'm a cat
> print(type(Tom))
string
string块用两个方括号括起来可以换行,换行之后前面会变成两个大于号>>,直到]]封尾
> Tom=[[I'm a cat; >> I hate Spike]] > print(Tom) I'm a cat;
I hate Spike
拼接字符串,使用 … 两个英文句号拼接字符串
> Tom="I'm a cat;"
> Jerry="I'm a mouse;"
> print(Tom .. Jerry) --使用..来拼接字符串
I'm a cat;I'm a mouse;
延伸:注释
-- 为lua单行注释
--[[
这是多行注释
--]]
boolean
> print(type(true))
boolean
> print(type(false))
boolean
> if false or nil
>> then
>> print("至少有一个是true")
>> else
>> print("false和nil都是false")
>> end
false和nil都是false
number
lua可以对number类型的数据进行运算
> a=12
> b=34
> c=a+b
> d=a-b
> e=a/b
> print(c)
46
> print(d)
-22
> print(e)
0.35294117647059
> print(type(c))
number
> print(type(d))
number
> print(type(e))
number
table
table有点像java中的map可以自定义key值,但是不定义key值的话key默认从1开始索引
> table1={
key="val1",key2="val2",key3="val3"}
> print(type(table1))
table
> print(table1["key"])
val1
> print(table1["key3"])
val3
> table2={
"Tom","Jerry","Spike"}
> print(type(table2))
table
> print(table2[0])
nil
> print(table2[1])
Tom
> print(table2[2])
Jerry
写个脚本table1.lua,for循环中的k,v是自定义的,可以写任何名称a,b;key,val···目的就是代表q前一个是索引,后一个是值,pairs()里面放要循环的table名
#!/bin/lua
table1={
}
table1["key"]="value"
key = 5
table1[key] = 15
table1[key] = table1[key] + 10
for k,v in pairs(table1) do
print(k .. ":" .. v)
end
执行结果
[[email protected] ~]# lua table1.lua
key:value
5:25
function
在 Lua 中,函数是被看作是"第一类值(First-Class Value)",函数可以存在变量里:
写一个脚本function_test.lua,这个脚本内部相当于递归调用自己的方法进行阶乘的运算
#!/bin/lua
function factorial1(n)
if n == 0 then
return 1
else
return n*factorial1(n-1)
end
end
print(factorial1(5))
factorial2=factorial1
print(factorial2(5))
执行结果
[[email protected] ~]# lua function_test.lua
120
120
function 可以以匿名函数(anonymous function)的方式通过参数传递,写一个脚本function_test2.lua
#!/bin/lua
function testFun(tab,fun)
for key,val in pairs(tab) do
print(fun(key,val))
end
end
tab={
key1="val1",key2="val2"}
testFun(tab,
function(key,val) --匿名函数
return key.."="..val
end
)
执行结果
[[email protected] ~]# lua function_test2.lua
key1=val1
key2=val2
thread
在 Lua 里,最主要的线程是协同程序(coroutine)。它跟线程(thread)差不多,拥有自己独立的栈、局部变量和指令指针,可以跟其他协同程序共享全局变量和其他大部分东西
线程跟协程的区别:线程可以同时多个运行,而协程任意时刻只能运行一个,并且处于运行状态的协程只有被挂起(suspend)时才会暂停
userdata
userdata 是一种用户自定义数据,用于表示一种由应用程序或 C/C++ 语言库所创建的类型,可以将任意 C/C++ 的任意数据类型的数据(通常是 struct 和 指针)存储到 Lua 变量中调用
边栏推荐
- Summary of Arthas vmtool command
- Redis学习笔记—持久化机制之RDB
- 6月《中國數據庫行業分析報告》發布!智能風起,列存更生
- How to use "tomato working method" in flowus, notation and other note taking software?
- Unique paths II of leetcode topic analysis
- (resolved) difference between leftmost prefix and overlay index
- Node request module cookie usage
- RGB与CMYK颜色模式
- 自定义标签——jsp标签增强
- Longest substring without repeated characters (C language)
猜你喜欢
随机推荐
65. Valid Number
TDesign update weekly report (the first week of January 2022)
4sum of leetcode topic analysis
A method of realizing video call and interactive live broadcast in small programs
如何在 FlowUs、Notion 等笔记软件中使用矩阵分析法建立你的思维脚手架
通用分页(1)
Cookie和Session入门
Spirit matrix for leetcode topic analysis
Leetcode topic analysis 3sum closest
力扣之滑动窗口《循序渐进》(209.长度最小的子数组、904. 水果成篮)
36 krypton launched | cloud native database company "tuoshupai" completed a new round of strategic financing, and the valuation has reached the level of quasi Unicorn
三层架构与SSM之间的对应关系
Happy number of leetcode topic analysis
670. Maximum Swap
1、 Software architecture evaluation
What exactly is RT?
An idea of using keep alive to cache data in vue3 form pages
一元函数求极限三大方法---洛必达法则,泰勒公式
扫码登录基本流程
Best time to buy and sell stock II








