LUA的学习

Lua的编写方法:

在 Lua 中,变量默认是全局变量,除非你使用 local 明确声明为局部变量。

在 Lua 中,函数的编写使用 `function` 关键字,语法如下:

function 函数名 (参数列表)

-- 函数体

return 返回值(可返回多个值)

end

示例:

  1. 无参数无返回值的函数
Lua 复制代码
   function sayHello()

       print("Hello, world!")

   end

sayHello() -- 调用函数

  1. 带参数无返回值的函数
Lua 复制代码
   function greet(name)

       print("Hello, " .. name .. "!")

   end

greet("apple") -- 输出: Hello,apple!

  1. 带参数带返回值的函数
Lua 复制代码
   function add(a, b)

       return a + b

   end



   local result = add(3, 5)

   print(result)  -- 输出: 8
  1. 可变参数函数
Lua 复制代码
   function sum(...)

       local args = {...}

       local total = 0

       for i, v in ipairs(args) do

           total = total + v

       end

       return total

   end



   print(sum(1, 2, 3, 4))  -- 输出: 10

匿名函数(函数作为值)

Lua 支持将函数赋值给变量,或者作为参数传递给其他函数:

Lua 复制代码
local multiply = function(a, b)

    return a * b

end



print(multiply(2, 3))  -- 输出: 6

递归函数

Lua 复制代码
function factorial(n)

    if n == 0 then

        return 1

    else

        return n * factorial(n - 1)

    end

end



print(factorial(5))  -- 输出: 120

-- 逻辑控制:

Lua 复制代码
a = 0

b = 5

while (a < 10) do

    if (a > b)

    then

        print("a is greater than b")

    else

        print("a is less than b")

    end

    if (a == 8)

    then

        goto test

    end

    a = a + 1

end



::test::

local start = a

for a = start, 10, 1 do

    print("a=" .. a)

end

-- 数据

Lua 复制代码
-- table

table = { "city1", "city2", "city3" }

table[4] = "city4"



for i, city in ipairs(table) do

    print(i .. ": " .. city)

end



for i = 1, #table do

    print(i .. ": " .. table[i])

end



--print(table.unpack(table))



-- map

map = { key1 = "value1", key2 = "value2", key3 = "value3" }

map["key4"] = "value4"



for k, v in pairs(map) do

    print(k .. ":" .. v)

end



--mixed

mixed = { "city1", key1 = "value1", "city2", key2 = "value2", "city3", key3 = "value3" }

mixed[3] = "city4"

mixed[5] = "city5"

mixed["key4"] = "value4"



for i = 1, #mixed, 1 do

    print("mixed[" .. i .. "] == " .. mixed[i])

end

for k, v in pairs(mixed) do

    print(k .. " = " .. v)

end --实际上均为键值对,默认键名为数字,输出时顺序会打乱



-- (global) mixed: {

--     key1: string = "value1",

--     key2: string = "value2",

--     key3: string = "value3",

--     ["key4"]: string = "value4",

--     [1]: string = "city1",

--     [2]: string = "city2",

--     [3]: string = "city3"|"city4",

--     [5]: string = "city5",

-- }

--模块

package.path = package.path .. "./" -- 添加当前文件夹

local mymodule = require("mymodule") -- 加载模块

print(mymodule.VERSION) -- 输出模块变量

mymodule.publicFunc() -- 调用模块函数

相关推荐
chicheese42 分钟前
Linux 面试速查表:从初级到高级,附高频场景答题模板
linux·运维
是翎1 小时前
深度长文|2026产品经理AI实践手册
人工智能·深度学习·学习·自然语言处理·数据挖掘
Linux-lucky1 小时前
32-38-Linux学习之旅之MySQL综合
linux·运维·学习·mysql·ubuntu
vortex52 小时前
常用终端模拟器全面对比:Kitty、WezTerm、Ghostty 与 Konsole
linux
知识分享小能手2 小时前
C++ 学习教程,从入门到精通,C++ 入门知识 — 完整知识点(1)
开发语言·c++·学习
INGNIGHT3 小时前
270 · 电话号码的字母组合II(Trie)
linux·算法
zly35003 小时前
VMware Converter Standalone 物理机转化为虚拟机后源1硬盘变成了2个硬盘(2个硬盘文件)虚拟机无法启动。
linux·运维·服务器
GeW5 小时前
从内核到数据:Linux与工业数据库如何支撑高端制造
linux
AI职业加油站5 小时前
数据要素价值释放年:大数据治理工程师,站上职业新风口
人工智能·学习·职场和发展·数据分析·职场发展
啄缘之间5 小时前
1.【SVA】什么是SVA?
经验分享·学习·测试用例·verilog·sva