• Lua语法结构


    Lua基础

    注释

    print("hello.")
    -- 单行注释的写法
    --[[
    多行注释的写法
    --]]
    
    • 1
    • 2
    • 3
    • 4
    • 5

    标识符

    关键字

    **and **
    break
    **do **
    else
    **elseif **
    **end **
    false
    for
    **function **
    if
    in
    local
    nil
    not
    or
    repeat
    return
    then
    true
    until
    **while **

    数据类型

    image.png
    nil
    ** boolean**
    ** number**
    ** string**
    ** function**
    ** userdata**
    thread
    ** table**

    --科学计数法
    print(2e+1) -- 2x10^1
    print(2e-1)
    
    • 1
    • 2
    • 3

    ```lua

    –string
    print(“hello”…“world”);

    print(“1”…“2”)
    print(type(“1”…“2”)) – 12 string

    print(“1”+“2”)
    print(type(“1”+“2”)) --3 number

    --table
    
    table1 = {} --空表
    table2 = {f1 = 100,key2 = "value"}
    
    print(table1)
    print(table2.f1)
    print(table2.key2)
    print(table2["key2"])
    
    --使用索引来获取
    fruits = {"apple","pear","oranger"}
    for i,j in pairs(fruits) do
    	print(i.."|"..j)
    end
    print(fruits[1])
    print(fruits[2])
    print(fruits[3])
    
    --table是自动扩容的
    
    fruits[1] = "newApple"
    print(fruits[1])
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    table的索引是从1开始

    函数

    --函数
    --函数
    
    function fact(n)
    	if n == 1 
      then
    		return n
    	else
    		return n*fact(n-1)
    	end
    end
    
    print(fact(5))
    
    
    function ReadMap(table,func)
    	for k,v in pairs(table) do
    		func(k,v)
    	end
    end
    
    
    function func(k,v)
    	print("key: "..k)
    	print("value: "..v)
    end
    
    
    fruits = {apple = 5,banana = 10,watermelon = 20}
    ReadMap(fruits,func)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    匿名函数

    myprint = function(param)
    	printFunc(param)
    end
    
    function printFunc(param)
    	print("打印:"..param)
    end
    
    myprint(90)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    param : 里面是一个table

    变量

    lua中变量的类型是可以更改的

    a = 5 -- 全局变量
    print(type(a))
    
    
    a = "hello World"
    print(type(a))
    
    
    do
    	local b = 10
    	print(a)
    	print(b) -- 10
    end
    
    print(a)
    print(b) -- nil
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    多变量赋值

    function test(a,b)
    	return a,b
    end
    
    
    a,b = test(1,2)
    print(a..b)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    循环

    a = 0
    while(a < 5) do
    	if(a%2 == 0) then
    		print(a)
    	end
    	a = a+1
    end
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    for i=0,10,1 do
    	print(i)
    end
    
    --repeat
    
    i = 2
    repeat
    	print(i)
    	i = i*2
    until(i > 10) --循环截至条件
    
    
    for i =1,10,1 do
    	j=1
    	while j <= i do
    		print(j)
    		j= j+1
    	end
    end
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    流程控制

    a = 7
    
    if(a>10) then
    	print("a大于10")
    elseif a > 5 then
    	print("a大于5")
    elseif a > 5 then
    	print("a大于0")
    else
    	print("a小于0")
    end
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    运算符

    ~=不等于

    转义字符

    字符串操作

    str = "My name is huangjiaqi"
    str2 = string.upper(str)
    str3 = string.gsub(str,"i","I")
    
    index = string.find(str,"name")
    str4 = string.reverse(str);
    
    print(str)
    print(str2)
    print(str3)
    print(index) --索引从1开始
    print(str4)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    字符串格式化

    image.png

    --字符串格式化
    
    
    num1 = 3 ; num2 = 5
    str5 = string.format("加法运算:%d+%d = %d",num1,num2,(num1+num2))
    print(str5)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 相关阅读:
    2024年AI辅助研发:科技遇上创意,无限可能的绽放
    LVS+Keepalived高可用群集
    Map集合详细讲解
    ConcurrentHashMap put和扩容的源码深度解析(内含JDK8中3个bug以及修复的版本)
    linux之top、ps、free命令详解
    日本语自然语言处理中的分词库 - GiNZA
    嵌入式系统日志轮转:实现与性能考量
    桥接模式-C++实现
    #{}和${}的区别
    Dubbo使用invoke指令来调用dubbo接口
  • 原文地址:https://blog.csdn.net/Mr_PEaSutBUtteR/article/details/132703115