当前位置:网站首页>lua学习
lua学习
2022-08-05 02:29:00 【qq_40707462】
notepad 编码方式 ansi
cmd:lua54.exe 文件全名.lua
一、数据结构

1、table
遍历
tab1 = {
key1 = "val1", key2 = "val2", "val3" }
for k, v in pairs(tab1) do
print(k .. " - " .. v)
end
-- 给全局变量或者 table 表里的变量赋一个 nil 值,等同于把它们删掉
tab1.key1 = nil
for k, v in pairs(tab1) do
print(k .. " - " .. v)
end
结果
1 - val3
key1 - val1
key2 - val2
1 - val3
key2 - val2
新建表
-- 创建一个空的 table
local tbl1 = {
}
-- 直接初始表
local tbl2 = {
"apple", "pear", "orange", "grape"}
索引
Lua 中的表(table)其实是一个"关联数组"(associative arrays),数组的索引可以是数字或者是字符串。
默认初始索引一般以 1 开始
a = {
}
a["key"] = "value"
key = 10
a[key] = 22
a[key] = a[key] + 11
for k, v in pairs(a) do
print(k .. " : " .. v)
end
结果
key : value
10 : 33
一些操作
2、基本操作
nil比较要加双引号,type(X) 实质是返回的是一个 string 类型
type(X)==nil
false
type(X)=="nil"
true
#来计算字符串的长度
> len = "www.runoob.com"
> print(#len)
14
字符连接 ..
> print("a" .. 'b')
ab
> print(157 .. 428)
157428
3、if-else,boolean
if false or nil then
print("至少有一个是 true")
else
print("false 和 nil 都为 false")
end
if 0 then
print("数字 0 是 true")
else
print("数字 0 为 false")
end
false 和 nil 都为 false
数字 0 是 true
4、number
print(type(2))
print(type(2.2))
print(type(0.2))
print(type(2e+1))
print(type(0.2e-1))
print(type(7.8263692594256e-06))
在对一个数字字符串上进行算术操作时,Lua 会尝试将这个数字字符串转成一个数字
> print("2" + 6)
8.0
> print("2" + "6")
8.0
> print("2 + 6")
2 + 6
> print("-2e2" * "6")
-1200.0
5、string
https://www.runoob.com/lua/lua-strings.html
用 " " 或者 [[ ]]
html = [[ <html> <head></head> <body> <a href="http://www.runoob.com/">菜鸟教程</a> </body> </html> ]]
print(html)
6、函数
-- function_test.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))
$ lua function_test.lua
120
120
匿名函数
-- function_test2.lua 脚本文件
function testFun(tab,fun)
for k ,v in pairs(tab) do
print(fun(k,v));
end
end
tab={
key1="val1",key2="val2"};
testFun(tab,
function(key,val)--匿名函数
return key.."="..val;
end
);
可变参数
function average(...)
result = 0
local arg={
...} --> arg 为一个表,局部变量
for i,v in ipairs(arg) do
result = result + v
end
print("总共传入 " .. #arg .. " 个数")
return result/#arg
end
print("平均值为",average(10,5,3,4,5,6))
7、变量赋值
local 的作用域到语句块结束
do
local a = 6 -- 局部变量
b = 6 -- 对局部变量重新赋值
print(a,b); --> 6 6
end
print(a,b) --> 5 6
table索引
t[i]
t.i
a = "hello" .. "world"
t.n = t.n + 1
-- 交换,个数不同时,按变量个数补足nil,或多余的值会被忽略
x, y = y, x
a, b, c = 0
print(a,b,c) --> 0 nil nil
-- 函数多返回值
a, b = f()
二、循环
1、while 循环
在条件为 true 时,让程序重复地执行某些语句。执行语句前会先检查条件是否为 true。
a=10
while( a < 20 )
do
print("a 的值为:", a)
a = a+1
end
2、for 循环
重复执行指定语句,重复次数可在 for 语句中控制。
var 从 exp1 变化到 exp2,每次变化以 exp3 为步长递增 var,并执行一次 “执行体”。exp3 是可选的,如果不指定,默认为1。
for var=exp1,exp2,exp3 do
<执行体>
end
for的三个表达式在循环开始前一次性求值,以后不再进行求值。比如上面的f(x)只会在循环开始前执行一次,其结果用在后面的循环中。
function f(x)
print("function")
return x*2
end
for i=1,f(5) do print(i)
end
结果:function 只打印一次
function
1
2
3
4
5
6
7
8
9
10
循环数组
a = {
"one", "two", "three"}
for i, v in ipairs(a) do
print(i, v)
end
3、repeat…until
重复执行循环,直到 指定的条件为真时为止
a = 10
repeat
print("a的值为:", a)
a = a + 1
until( a > 15 )
a的值为: 10
a的值为: 11
a的值为: 12
a的值为: 13
a的值为: 14
a的值为: 15
4、循环嵌套
可以在循环内嵌套一个或多个循环语句(while do … end;for … do … end;repeat … until;)
j =2
for i=2,10 do
for j=2,(i/j) , 2 do
if(not(i%j))
then
break
end
if(j > (i/j))then
print("i 的值为:",i)
end
end
end
i 的值为: 8
i 的值为: 9
i 的值为: 10
5、break
a = 10
--[ while 循环 --]
while( a < 20 )
do
print("a 的值为:", a)
a=a+1
if( a > 15)
then
break
end
end
6、goto、continue
i = 0
::s1:: do
print(i)
i = i+1
end
if i>3 then
os.exit() -- i 大于 3 时退出
end
goto s1
结果
0
1
2
3
continue
for i=1, 3 do
if i <= 2 then
print(i, "yes continue")
goto continue
end
print(i, " no continue")
::continue::
print([[i'm end]])
end
1 yes continue
i'm end
2 yes continue
i'm end
3 no continue
i'm end
7、无限循环
while( true )
do
print("循环将永远执行下去")
end
8、if-else
if( 布尔表达式 1)
then
--[ 在布尔表达式 1 为 true 时执行该语句块 --]
elseif( 布尔表达式 2)
then
--[ 在布尔表达式 2 为 true 时执行该语句块 --]
elseif( 布尔表达式 3)
then
--[ 在布尔表达式 3 为 true 时执行该语句块 --]
else
--[ 如果以上布尔表达式都不为 true 则执行该语句块 --]
end
三、面向对象
我们可以使用点号(.)来访问类的属性:
print(r.length)
我们可以使用冒号 : 来访问类的成员函数:
r:printArea()
-- 元类
Shape = {
area = 0}
-- 基础类方法 new
function Shape:new (o,side)
o = o or {
}
setmetatable(o, self)
self.__index = self
side = side or 0
self.area = side*side;
return o
end
-- 基础类方法 printArea
function Shape:printArea ()
print("面积为 ",self.area)
end
-- 创建对象
myshape = Shape:new(nil,10)
myshape:printArea()
继承
-- Meta class
Shape = {
area = 0}
-- 基础类方法 new
function Shape:new (o,side)
o = o or {
}
setmetatable(o, self)
self.__index = self
side = side or 0
self.area = side*side;
return o
end
-- 基础类方法 printArea
function Shape:printArea ()
print("面积为 ",self.area)
end
-- 创建对象
myshape = Shape:new(nil,10)
myshape:printArea()
Square = Shape:new()
-- 派生类方法 new
function Square:new (o,side)
o = o or Shape:new(o,side)
setmetatable(o, self)
self.__index = self
return o
end
-- 派生类方法 printArea
function Square:printArea ()
print("正方形面积为 ",self.area)
end
-- 创建对象
mysquare = Square:new(nil,10)
mysquare:printArea()
Rectangle = Shape:new()
-- 派生类方法 new
function Rectangle:new (o,length,breadth)
o = o or Shape:new(o)
setmetatable(o, self)
self.__index = self
self.area = length * breadth
return o
end
-- 派生类方法 printArea
function Rectangle:printArea ()
print("矩形面积为 ",self.area)
end
-- 创建对象
myrectangle = Rectangle:new(nil,10,20)
myrectangle:printArea()
面积为 100
正方形面积为 100
矩形面积为 200
边栏推荐
- 学习笔记-----左偏树
- 直播回放含 PPT 下载|基于 Flink & DeepRec 构建 Online Deep Learning
- 使用SuperMap iDesktopX数据迁移工具迁移ArcGIS数据
- 树表的查找
- 短域名绕过及xss相关知识
- 亚马逊云科技携手中科创达为行业客户构建AIoT平台
- iNFTnews | 对体育行业和球迷来说,NFT可以带来什么?
- Programmer's list of sheep counting when insomnia | Daily anecdote
- 01 【前言 基础使用 核心概念】
- 【genius_platform软件平台开发】第七十六讲:vs预处理器定义的牛逼写法!!!!(其他组牛逼conding人员告知这么配置来取消宏定义)
猜你喜欢

Intel XDC 2022 Wonderful Review: Build an Open Ecosystem and Unleash the Potential of "Infrastructure"

【MySQL series】- Does LIKE query start with % will make the index invalid?

【C语言】详解栈和队列(定义、销毁、数据的操作)
![[ROS] (10) ROS Communication - Service Communication](/img/4d/4657f24bd7809abb4bdc4b418076f7.png)
[ROS] (10) ROS Communication - Service Communication
![[Unity Entry Plan] Handling of Occlusion Problems in 2D Games & Pseudo Perspective](/img/de/944b31c68cc5b9ffa6a585530e7be9.png)
[Unity Entry Plan] Handling of Occlusion Problems in 2D Games & Pseudo Perspective

没有对象的程序员如何过七夕

Jincang database KingbaseES V8 GIS data migration solution (3. Data migration based on ArcGIS platform to KES)

多线程(2)

RAID disk array

KingbaseES V8 GIS data migration solution (2. Introduction to the capabilities of Kingbase GIS)
随机推荐
编译预处理等细节
EBS uses virtual columns and hint hints to optimize sql case
C语言实现简单猜数字游戏
如何模拟后台API调用场景,很细!
力扣-相同的树
leetcode 15
使用SuperMap iDesktopX数据迁移工具迁移地图文档和符号
iNFTnews | 对体育行业和球迷来说,NFT可以带来什么?
.Net C# 控制台 使用 Win32 API 创建一个窗口
Chapter 09 Use of Performance Analysis Tools [2. Index and Tuning] [MySQL Advanced]
Opening - Open a new .NET modern application development experience
海量服务实例动态化管理
select tag custom style
线性表的查找
迁移学习——Distant Domain Transfer Learning
Matlab map with color representation module value size arrow
C language implements a simple number guessing game
关于#sql shell#的问题,如何解决?
Leetcode brushing questions - 22. Bracket generation
Error: Not a signal or slot declaration