当前位置:网站首页>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
边栏推荐
- Greenplum Database Fault Analysis - Why Does gpstart -a Return Failure After Version Upgrade?
- CPDA|运营人如何从负基础学会数据分析(SQL)
- select tag custom style
- [Decryption] Can the NFTs created by OpenSea for free appear in my wallet without being chained?
- 基于OpenVINO工具套件简单实现YOLOv7预训练模型的部署
- How do programmers without objects spend the Chinese Valentine's Day
- Understand the recommendation system in one article: Recall 06: Two-tower model - model structure, training method, the recall model is a late fusion feature, and the sorting model is an early fusion
- 【存储】曙光存储DS800-G35 ISCSI各映射LUN给服务器
- 回顾51单片机
- C student management system Insert the student node at the specified location
猜你喜欢
1349. Maximum number of students taking the exam Status Compression
Quickly learn chess from zero to one
select tag custom style
常见的硬件延迟
Transfer Learning - Joint Geometrical and Statistical Alignment for Visual Domain Adaptation
直播预告|30分钟快速入门!来看可信分布式AI链桨的架构设计
[Decryption] Can the NFTs created by OpenSea for free appear in my wallet without being chained?
The 2022 EdgeX China Challenge will be grandly opened on August 3
shell语句修改txt文件或者sh文件
使用SuperMap iDesktopX数据迁移工具迁移ArcGIS数据
随机推荐
散列表的查找(哈希表)
.Net C# 控制台 使用 Win32 API 创建一个窗口
sql语句多字段多个值如何进行排序
KingbaseES V8 GIS data migration solution (2. Introduction to the capabilities of Kingbase GIS)
[ROS] (10) ROS Communication - Service Communication
select tag custom style
基于OpenVINO工具套件简单实现YOLOv7预训练模型的部署
C语言实现简单猜数字游戏
the mechanism of ideology
LPQ(局部相位量化)学习笔记
力扣-相同的树
如何模拟后台API调用场景,很细!
开篇-开启全新的.NET现代应用开发体验
后期学习计划
使用OpenVINO实现飞桨版PGNet推理程序
leetcode 15
亚马逊云科技携手中科创达为行业客户构建AIoT平台
Live preview | 30 minutes started quickly!Look at credible distributed AI chain oar architectural design
Intel XDC 2022 Wonderful Review: Build an Open Ecosystem and Unleash the Potential of "Infrastructure"
协作D2D局部模型聚合的半分散联合学习