Skip to content

Lua 速查表

Lua 是魔兽世界的 UI 和插件使用的脚本语言。要进行插件开发,首先需要了解 Lua 的基本语法。本文提供了一个速查表。

注意

魔兽世界中的 Lua 是 Lua 5.1 版本的子集,不支持所有的 Lua 语法。

下面是一些更详细的 Lua 教程资源:

注释

lua
-- 单行注释
--[[
  多行注释
]]
--[==[
  多行注释(对称的任意数量的等号)
]==]

数据类型

lua
print(type(123))     -- number
print(type("hello")) -- string
print(type(true))    -- boolean
print(type(nil))     -- nil
print(type(print))   -- function
print(type({}))      -- table

布尔运算和短路求值

lua
print(1 == 2)        -- false
print(1 ~= 2)        -- true
print(1 < 2)         -- true
print(1 <= 2)        -- true
print(1 == "1")      -- false  不同类型不会自动转换

-- 只有 nil 和 false 会被认为是 false ,其他值都为 true
print(not not false) -- false
print(not not nil)   -- false
print(not not true)  -- true
print(not not 0)     -- true
print(not not "")    -- true

-- 逻辑运算符,以及短路求值
-- and 运算符,左侧为 true 时,结果为右侧,否则为左侧
print(true and 3)    -- 输出 3
print(4 and 5)       -- 输出 5
print(false and 3)   -- 输出 false
print(nil and 2)     -- 输出 nil
-- or 运算符,左侧为 true 时,结果为左侧,否则为右侧
print(true or 3)     -- 输出 true
print(4 or 5)        -- 输出 4
print(false or 3)    -- 输出 3
print(nil or 2)      -- 输出 2

-- 配合使用 and 和 or 运算符,可以实现条件判断,类似三元运算符
print(a and 1 or 2)  -- 若 a 为真,则输出 1,否则输出 2
if a then print(1) else print(2) end -- 与上一行等价

数学运算

lua
print(2 + 3 * 4)   -- 14
print((2 + 3) * 4) -- 20
print(2 + "3")     -- 5  字符串自动转换为数字

print(math.max(23, 17)) -- 23
print(math.floor(2.9)) -- 2
print(math.cos(2 * math.pi)) -- 1

print(math.pow(2, 10)) -- 1024
print(2^10)            -- 1024
print(math.fmod(14, 3)) -- 2
print(14%3)             -- 2

字符串操作

lua
'字符串' .. '拼接'

s = "Hello"
s:upper()
s:lower()
s:len()    -- 等同于 #s

s:find()
s:gfind()

s:match()
s:gmatch()

s:sub()
s:gsub()

s:rep()
s:char()
s:dump()
s:reverse()
s:byte()
s:format()

查找和替换

lua
local s = "Hello my friend"
print(string.sub(s, 7))         -- my friend
print(string.gsub(s, "l", "w")) -- Hewwo my friend, 2

-- pattern,与正则表达式略微不同
print(string.match("abcdefg", "b..")) -- bcd (. 匹配任意字符)
print(string.find("abcdefg", "b.."))  -- 2, 4

print(string.match("foo 123 bar", "%d%d%d")) -- 123 (%d 匹配数字)
print(string.match("hello World", "%u")) -- W (%u 匹配大写字母)

-- 捕获
local m, d, y = string.match("04/19/64", "(%d+)/(%d+)/(%d+)")
print(m, d, y) -- 04, 19, 64

控制流

条件语句

lua
local x = math.random(1, 6)

if x == 3 then
  print(x, "is three")
elseif x < 3 then
  print(x, "is less than three")
else
  print(x, "is greater than three")
end

for 循环

lua
local sum = 0

for i = 1, 10 do
  sum = sum + i
end

print(sum) -- 55

while 循环

lua
local sum, i = 0, 0

while true do
  i = i + 1
  sum = sum + i
  if i == 10 then
    break
  end
end

print(sum) -- 55

函数

lua
function square(v)
  return v * v
end

print(square(4)) -- 16

动态参数

lua
function add(...)
  local sum = 0
  for i, v in ipairs({...}) do
    sum = sum + v
  end
  return sum
end

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

select 函数

lua
function foo(...)
  print(select("#", ...)) -- 3
  print(select(1, ...))   -- a, b, c
  print(select(2, ...))   -- b, c
  print(select(3, ...))   -- c

  print((select(2, ...))) -- b
end
foo("a", "b", "c")

语法糖

lua
a = function() end
-- 等价于
function a() end
lua
local t = {}

-- 定义时:
t.a = function(t) print(t) end
-- 等价于
function t.a(t) print(t) end
-- 等价于
function t:a() print(self) end

-- 调用时:
t.a(t)
-- 等价于
t:a()

lua
table.foreach(t, function(row) ... end)
table.insert(t, 21)
table.insert(t, 4, 99)
table.concat
table.sort
table.remove(t, 4)

全局API

lua
type(var)   -- "nil" | "number" | "string" | "boolean" | "table" | "function" | "thread" | "userdata"

-- Does /not/ invoke meta methods (__index and __newindex)
rawset(t, index, value)    -- Like t[index] = value
rawget(t, index)           -- Like t[index]

_G  -- Global context
setfenv(1, {})  -- 1: current function, 2: caller, and so on -- {}: the new _G

pairs(t)     -- iterable list of {key, value}
ipairs(t)    -- iterable list of {index, value}

tonumber("34")
tonumber("8f", 16)