当前位置:网站首页>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
边栏推荐
- 【C语言】详解栈和队列(定义、销毁、数据的操作)
- 01 【前言 基础使用 核心概念】
- ".NET IoT from scratch" series
- VSCode Change Default Terminal 如何修改vscode的默认terminal
- LeetCode uses the minimum cost to climb the stairs----dp problem
- 【genius_platform软件平台开发】第七十六讲:vs预处理器定义的牛逼写法!!!!(其他组牛逼conding人员告知这么配置来取消宏定义)
- hypervisor相关的知识点
- 如何基于OpenVINO POT工具简单实现对模型的量化压缩
- 树表的查找
- The 2022 EdgeX China Challenge will be grandly opened on August 3
猜你喜欢
CPDA|运营人如何从负基础学会数据分析(SQL)
sql语句多字段多个值如何进行排序
Leetcode brushing questions - 22. Bracket generation
[Decryption] Can the NFTs created by OpenSea for free appear in my wallet without being chained?
J9数字货币论:web3的创作者经济是什么?
释放技术创新引擎,英特尔携手生态合作伙伴推动智慧零售蓬勃发展
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
01 [Foreword Basic Use Core Concepts]
“嘀哩哩,等灯等灯”,工厂安全生产的提示音
特殊矩阵的压缩存储
随机推荐
RAID磁盘阵列
迁移学习——Joint Geometrical and Statistical Alignment for Visual Domain Adaptation
基于OpenVINO工具套件简单实现YOLOv7预训练模型的部署
C语言实现简单猜数字游戏
“嘀哩哩,等灯等灯”,工厂安全生产的提示音
散列表的查找(哈希表)
02 [Development Server Resource Module]
力扣-二叉树的前序遍历、中序遍历、后序遍历
直播预告|30分钟快速入门!来看可信分布式AI链桨的架构设计
SDC简介
亚马逊云科技 + 英特尔 + 中科创达为行业客户构建 AIoT 平台
2022了你还不会『低代码』?数据科学也能玩转Low-Code啦!
【MySQL系列】- LIKE查询 以%开头一定会让索引失效吗
Industry case | insurance companies of the world's top 500 construction standards can be used to drive the business analysis system
Jincang database KingbaseES V8 GIS data migration solution (3. Data migration based on ArcGIS platform to KES)
Advanced Numbers_Review_Chapter 1: Functions, Limits, Continuity
特殊矩阵的压缩存储
[ROS](10)ROS通信 —— 服务(Service)通信
Utilities 网络安全与元宇宙:找出薄弱环节