天天看點

Lua快速入門

可參考的教程: https://www.runoob.com/lua/lua-tutorial.html

一、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、嵌入性

二、開發環境

http://www.lua.org/

Mac

$ brew install lua

$ lua -v
Lua 5.3.5      

hello.lua

print("hello world")
-- hello world


for 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 = 1
b = true
print(a, b)
-- 1   true      

Lua的Table

Table = 數組 + 映射

下标是從1開始

沒有類型限制

大小自動擴容

數組寫法

寫法一:
a = {}
a[1] = 10
a[2] = 20
a[3] = "hello"

print(a[1], a[2], a[3])
-- 10  20  hello

寫法二:
a = {
    10,
    20,
    "hello"
}
      

映射寫法

寫法一:
a = {}
a["hello"] = 2
a[3] = false

print(a.hello, a[3])
print(a["hello"], a[3])

寫法二:
a = {
    ["hello"] = 2,
    [3] = false,
}      

四、Lua函數

function add(a, b)
    return a + b
end

print(add(1, 2))
-- 3


-- 函數可以在變量之間指派
add = function (a, b)
    return a + b
end


-- 傳回多個值
function add(a, b)
    return a + b, a - b
end

print(add(1, 2))
-- 3   -1

-- 多變量指派
a, b = 1, 2
print(a, b)
-- 1   2

-- 變量交換
a, b = b, a
print(a, b)
-- 2   1

-- 左邊變量多
a, b = 1
print(a, b)
-- 1   nil

-- 右邊變量多
a, b = 1, 2, 3
print(a, b)
-- 1   2      

五、Lua表達式

算術表達式

a = 1 + 1
print(2)

a = 1
b = 1
print((a + b) * 3)

-- 沒有++運算符
c = 1
c = c + 1
print(c)      

邏輯表達式

true and false => false
true or false => true
not false => true      

字元串拼接

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

作用域

-- 預設為全局變量
function foo()
    a = 1
end

foo()
print(a)
-- 1

-- local聲明為局部變量
function foo()
    local a = 1
end

foo()
print(a)
-- nil      

推薦:local大法好

六、Lua流程控制

if 和 while

if condition then
    ...
elseif condition then
    ...
else
    ...
end      
local i = 0

while i < 10 do
    print(i)
    i = i + 1
end      

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 + b
end

-- 等價于
-- class.foo = function (a, b)
--     return a + b
-- end

return 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: 0x7fd30f600000

print("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)
end

for k, v in pairs(t) do
    print(k, v)
end

for k, v in pairs(table) do
    print(k, v)
end


for k, v in pairs(table) do
    print(k, v)
end
--[[
unpack
move
pack  
sort  
concat
insert(table, value)
remove(table, index)
]]      

删除映射

local t = {}

t.a = 1
t.b = 2
t.a = nil

for k, v in pairs(t) do
    print(k, v)
end      

擷取長度

local t = {1, 2, 3}
print(#t)
-- 3

local s = "hello world"
print(#s)
-- 11      

類型轉換

local t = “hello”

print(type(t))

tonumber(“3.14”)

tostring(3.14)

字元串格式化

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

總結

Lua特點

數組下标從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