当前位置:网站首页>lua learning
lua learning
2022-08-05 02:31: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、基本操作
nilCompare with double quotes,type(X) Essentially what is returned is one 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 is scoped to the end of the statement block
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,or redundant values are ignored
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
边栏推荐
- [Decryption] Can the NFTs created by OpenSea for free appear in my wallet without being chained?
- 用@Mapper查询oracle的分区情况报错
- iNFTnews | 对体育行业和球迷来说,NFT可以带来什么?
- ARM Mailbox
- 甘特图来啦,项目管理神器,模板直接用
- select 标签自定义样式
- Flink 1.15.1 集群搭建(StandaloneSession)
- 2022了你还不会『低代码』?数据科学也能玩转Low-Code啦!
- SuperMap iDesktop.Net之布尔运算求交——修复含拓扑错误复杂模型
- 使用SuperMap iDesktopX数据迁移工具迁移地图文档和符号
猜你喜欢

matlab绘制用颜色表示模值大小的箭头图

C语言实现简单猜数字游戏

Tree search (bintree)

【OpenCV 图像处理2】:OpenCV 基础知识

2022-08-04:输入:去重数组arr,里面的数只包含0~9。limit,一个数字。 返回:要求比limit小的情况下,能够用arr拼出来的最大数字。 来自字节。

CPDA|运营人如何从负基础学会数据分析(SQL)

Leetcode brushing questions - 22. Bracket generation

Access Characteristics of Constructor under Inheritance Relationship
ROS通信 —— 服务(Service)通信](/img/4d/4657f24bd7809abb4bdc4b418076f7.png)
[ROS](10)ROS通信 —— 服务(Service)通信

LPQ (local phase quantization) study notes
随机推荐
Jincang database KingbaseES V8 GIS data migration solution (3. Data migration based on ArcGIS platform to KES)
Greenplum数据库故障分析——能对数据库base文件夹进行软连接嘛?
LPQ (local phase quantization) study notes
DAY22:sqli-labs 靶场通关wp(Less01~~Less20)
【日常训练】1403. 非递增顺序的最小子序列
注意潍坊开具发票一般需要注意
【OpenCV 图像处理2】:OpenCV 基础知识
Semi-Decentralized Federated Learning for Cooperative D2D Local Model Aggregation
转:查尔斯·汉迪:你是谁,比你做什么更重要
多线程(2)
Simple implementation of YOLOv7 pre-training model deployment based on OpenVINO toolkit
行业案例|世界 500 强险企如何建设指标驱动的经营分析系统
J9数字货币论:web3的创作者经济是什么?
Access Characteristics of Constructor under Inheritance Relationship
01 [Foreword Basic Use Core Concepts]
post-study program
How to simply implement the quantization and compression of the model based on the OpenVINO POT tool
sql语句多字段多个值如何进行排序
nodeJs--encapsulate routing
C语言实现简单猜数字游戏