Lua


字符串操作
https://www.runoob.com/lua/lua-strings.html
元方法
https://www.runoob.com/lua/lua-metatables.html

--data type
a1=20.5 --number,64位浮点
a2=true --boolean
a3=nil --nil
a4="123" --string
--a5,a6,a7,a8 --table --function

--type函数
print(type(a1)=="number") --true

--string operation
print(a4+2) --125
print(a4..a4) --123123
print(#a4) --3

--作用域:默认全局变量,作用域到块结束
b=1
function  f()
    local b=2
    print(b)
end
f() --2
print(b) --1

--赋值,多值赋值
a,b=1,2
print(a,b) --1 2
b,a=a,b
print(a,b) --2 1
a,b=1,2,3
print(a,b) --1 2
a,b=1
print(a,b) --1 nil
function f1()
    return 1,2
end
a,b=f1()
print(a,b) --1 2

--索引
a={}
a["index"]="value"
print(a["index"]) --value
print(a.index) --value

--循环
--for
function f(x)
    return x
end
for a=1,f(5),2 do
    print(a) --1 3 5
end

a={5,6,7}
for i,v in ipairs(a) do --ipairs用来遍历访问table,i->index 从1开始,v->value
    print(i,v) --1,5 2,6 3,7
end
--while
a=0
while(a<3) do
    print(a) --0 1 2
    a=a+1
end
--repeat
a=1
repeat
    a=a+1
    print(a) --2 3 4
until(a>3)
--break
a=1
while(a<5) do
    if(a>3) then
        break
    end
    a=a+1
    print(a) --2 3 4
end

--条件,nil,false为否,非nil,false为是,0也是是
a=0
if(a) then
    print("yes") --yes
end

a=0
if(a==1)then
    print(1)
else
    print(0) --0
end

a=2
if(a==1)then
    print(1)
elseif (a==2) then
    print(2) --2
else
    print(3)
end

--函数,默认全局
local function MyPrint(a,b,c)
    print(a,b,c)
end
MyPrint(1,2,3) --1 2 3

--函数作为参数
function PrintABC(a,b,c,printfunc)
    printfunc(a,b,c)
end
PrintABC(2,3,4,MyPrint) -- 2 3 4

--多值返回
function ReturnMore(a,b,c)
    return a,b,c
end
a,b=ReturnMore(1,2,3)
print(a,b) --1 2

--变长参数与select
--select(n,...)返回从n开始到结束的列表,不是table
--select("#",...)返回参数列表长度
function f(p,...)
    local a={select(2,...)}
    for i,v in ipairs(a)do
        print(i,v) --1 4
    end
    print(p) --2
    print(select("#",...)) --2
    return p,...
end
a,b=f(2,3,4)
print(a,b) --2 3 可以看到这里返回的是列表

--运算符
--算术运算符 +,-,*,/,%,^,-
print(1+2) --3
print(2-1) --1
print(2*3) --6
print(3/2) --1.5
print(3%2) --1
print(3^2) --9
print(-3) -- -3

--关系运算符 ==,~=,>=,<=,<,>
print(1==2) --false
print(1~=2) --true
print(2>=1) --true
print(2<=1) --false
print(1>2) --false
print(1<2) --true

--逻辑运算符 and,or,not
print(2>1 and 1<3) --true
function f()
    print("hello")
    return false
end
print(2>1 or f()) --true,f()没有计算
print(not(2>1))  --false

--其他运算符 .. #
a="hello ".." world" --连接
print(a) --hello  world
b=#a --长度
print(b) --12
array={[1]=2,[2]=3,[26]=4}
print(#array) --2 ,#在索引中断处停止计数了

--字符串,略
--数组
array={1,2,3}
for i=1,3 do
    print(array[i]) --1 2 3
end
--索引可以为负
array={}
for i=-2,2 do
    array[i]=i
end
for i=-2,0 do
    print(array[i])-- -2,1,0
end
array={}
for i=1,3 do
    array[i]={}
    for j=1,3 do
        array[i][j]=i*j
    end
end
for i=1,3 do
    for j=1,3 do
        print(array[i][j]) --1,2,3 2,4,6 3,6,9
    end
end

--迭代器
--无状态
--for in 函数,状态常量,变量
function it(array,index)
    index=index+1
    local v=array[index]
    if(v~=nil) then
        return index,v
    end

end
function myIpairs(array)
    return it,array,0
end
array={2,3,4}
for i,v in myIpairs(array) do
    print(i,v) --1,2 2,3 3,4
end

--多状态迭代器
array={2,3,4}
function it(array)
    local lenth=#array
    local index=0;
    return function()
        index=index+1
        if(index<=lenth) then
            return array[index]
        end
    end
end
for elem in it(array) do
    print(elem) --2 3 4
end
--table
array={1,a,2}
for i,v in ipairs(array) do
    print(i,v)
end

--值传递
local a=1
local b=a
a=nil
print(b)
--指针传递
local a={}
a[1]="asd"
b=a
print(b[1]) --asd
a[1]="bsd"
print(b[1]) --bsd

--table常用操作 concat,insert,remove,sort(升序)
t={"hello","wor","ld"}
print(table.concat(t,",",1,3)) --hello,wor,ld
table.insert(t,"!!!")
print(table.concat(t)) --helloworld!!!
table.remove(t,1)
print(table.concat(t)) --world!!!
t={2,3,1}
table.sort(t)
print(table.concat(t)) --123


--模块化
--"moduleTest.lua"
local moduleTest={}
moduleTest.a=1
return moduleTest

--“start.lua”
local mm=require("moduleTest")
print(mm.a) --1


--元表,元表有点像c++里的操作符重载,在特定条件下触发
--getmetatable,setmetatable
--__index,__newindex
t1={key1=1,key2=2}
t2={key3=3}
setmetatable(t1,{__index=t2}) --当表里没有该键时查看元表中的__index,index指向一个表,使得t1可以访问t2中的键
print(t2.key3) --3
setmetatable(t1,{__index=function (t,key)
    return 4
end})
print(t1.key4) --4 --当表里没有该键时查看元表中的__index,index指向一个函数,获得函数返回值

t1={key1=1,key2=2}
t2={}
setmetatable(t1,{__newindex=t2})
t1.key3=3
print(t1.key3,t2.key3) --nil 3 --当表里没有键时,为元表指向的表添加

-- __add 相加时触发函数
t1={1,2,3}
t2={4,5}
setmetatable(t1,{__add=function(first,second)
    for i=1,#second do
        table.insert(first,second[i])
    end
    return first
end})
t1=t1+t2
for i,v in ipairs(t1) do
    print(v) --1 2 3 4 5
end

--__tostring
t1={1,2,3}
setmetatable(t1,{__tostring=function(table)
    s=""
    for i,v in ipairs(table) do
        s=s..v
    end
    return s
end})
print(t1) --123
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 前言# 前两章我们总结了lua_is*系列和lua_to*系列,掌握了lua栈内值的判断和转换方法,现在我们来看看...
    AlbertS阅读 11,467评论 0 2
  • 前言# 前面几篇都是讲c/c++代码对lua文件中数据的读取和设置,这一篇我们来看看对数据类型的判断,这是非常有用...
    AlbertS阅读 11,970评论 1 3
  • 前言# 看了前几篇自己总结的文章,我发现了一个问题,那就是从标题上不能直接的看出文章所讲的内容,所以从这一章开始我...
    AlbertS阅读 13,190评论 0 6
  • 前言# 通过上一章所说的lua_is*系列,我们已经可以判定数值的类型,但是这不是我们的最终目的,我们的最终目的是...
    AlbertS阅读 12,815评论 0 5
  • 开篇 今天来简单了解一下 Lua 的函数调用:lua_call。 解析 函数调用协议 void lua_call ...
    码上说阅读 10,826评论 0 1

友情链接更多精彩内容