Lua快速入门
发布日期:2021-07-01 06:08:26 浏览次数:2 分类:技术文章

本文共 3226 字,大约阅读时间需要 10 分钟。

可参考的教程:

一、Lua简介

1、基本概念

类型与值
table
条件 if
循环 while for
包和库

2、执行速度

C : 1
Java : 1/2-1/3
JavaScriptV8 1/3-1/10
Lua: 1/30
PHP 1/30-1/100
Python 1/30-1/100

3、嵌入性

二、开发环境

Mac

$ brew install lua$ lua -vLua 5.3.5

hello.lua

print("hello world")-- hello worldfor i=1, 10 do    print(i)end-- 1 2 3 4 5 6 7 8 9 10

三、Lua值和类型

数值类型: 1, 1.2, 3.14

字符串类型: “hello world”
布尔类型: true false

变量赋值

a = 1b = trueprint(a, b)-- 1   true

Lua的Table

Table = 数组 + 映射
下标是从1开始
没有类型限制
大小自动扩容

数组写法

写法一:a = {}a[1] = 10a[2] = 20a[3] = "hello"print(a[1], a[2], a[3])-- 10  20  hello写法二:a = {    10,    20,    "hello"}

映射写法

写法一:a = {}a["hello"] = 2a[3] = falseprint(a.hello, a[3])print(a["hello"], a[3])写法二:a = {    ["hello"] = 2,    [3] = false,}

四、Lua函数

function add(a, b)    return a + bendprint(add(1, 2))-- 3-- 函数可以在变量之间赋值add = function (a, b)    return a + bend-- 返回多个值function add(a, b)    return a + b, a - bendprint(add(1, 2))-- 3   -1-- 多变量赋值a, b = 1, 2print(a, b)-- 1   2-- 变量交换a, b = b, aprint(a, b)-- 2   1-- 左边变量多a, b = 1print(a, b)-- 1   nil-- 右边变量多a, b = 1, 2, 3print(a, b)-- 1   2

五、Lua表达式

算术表达式

a = 1 + 1print(2)a = 1b = 1print((a + b) * 3)-- 没有++运算符c = 1c = c + 1print(c)

逻辑表达式

true and false => falsetrue or false => truenot false => true

字符串拼接

print("hello" .. "world")-- helloworld

作用域

-- 默认为全局变量function foo()    a = 1endfoo()print(a)-- 1-- local声明为局部变量function foo()    local a = 1endfoo()print(a)-- nil

推荐:local大法好

六、Lua流程控制

if 和 while

if condition then    ...elseif condition then    ...else    ...end
local i = 0while i < 10 do    print(i)    i = i + 1end

for数值遍历

for i = start, end, step do    ...end-- [0, 9]for i = 1, 9 do    print(i)end-- 等价于c语言for(int i = 0; i < 10; i++){    printf("%d\n", i);}

for泛型遍历

pairs 和 ipairs迭代器

数组|映射a = {    ["foo"] = 1,    [100] = true,    [1] = 20,    [2] = 30}-- 遍历的时候是无序的for k, v in pairs(a) do    print(k, v)end-- 100 true-- 1   20-- foo 1-- 2   30-- 遍历时只搜索数组部分for k, v in ipairs(a) do    print(k, v)end-- 1   20-- 2   30

七、Lua 包package

foo.lua

local class = {}function class.foo(a, b)    return a + bend-- 等价于-- class.foo = function (a, b)--     return a + b-- endreturn class

main.lua

local c = require("foo")print(c.foo(1, 2))

区别:

require 加载文件,只运行一次(最新)
dofile 加载并运行(早期)

print("require")for i = 1, 2 do    print(require("demo"))end-- require-- table: 0x7fd30f600000-- table: 0x7fd30f600000print("dofile")for i = 1, 2 do    print(dofile("demo.lua"))end-- dofile-- table: 0x7fd30f600360-- table: 0x7fd30f6007c0

执行字符串相当于eval

lua5.3之后不支持
dostring("")

Lua系统库

-- 注释--[[    长注释]]local t = {}for i = 1, 10 do    table.insert(t, 1)endfor k, v in pairs(t) do    print(k, v)endfor k, v in pairs(table) do    print(k, v)endfor k, v in pairs(table) do    print(k, v)end--[[unpackmovepack  sort  concatinsert(table, value)remove(table, index)]]

删除映射

local t = {}t.a = 1t.b = 2t.a = nilfor k, v in pairs(t) do    print(k, v)end

获取长度

local t = {1, 2, 3}print(#t)-- 3local s = "hello world"print(#s)-- 11

类型转换

local t = “hello”
print(type(t))

tonumber(“3.14”)

tostring(3.14)

字符串格式化

string.format(“hi%d”, 2)

总结

Lua特点

Table = 数组 + 映射
数组下标从1开始,数组连续使用否则是映射,自动扩展

Lua函数

万物皆值,函数也是一种值
函数支持多个返回值

Lua表达式

逻辑运算 and or not
字符串连接 …
local大法好 代码优化,作用域控制

Lua迭代器遍历

数组遍历 for k, v in ipairs(t) do…end
Table遍历 for k, v in pairs(t) do…end

转载地址:https://pengshiyu.blog.csdn.net/article/details/103359509 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Python数据库操作Orator-orm
下一篇:mongodb使用like模糊查询

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2024年04月30日 04时45分38秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章