Skip to content

Commit bf7867e

Browse files
author
mudssky
committed
docs(cheatsheet): 添加Lua语法速查表文档
新增Lua语法速查表,包含变量、控制结构、表操作、函数定义及常用标准库等内容,方便开发者快速查阅Lua语法要点
1 parent 290c442 commit bf7867e

1 file changed

Lines changed: 190 additions & 0 deletions

File tree

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
# Lua 语法速查表 (Cheatsheet)
2+
3+
## 1. 变量与基本数据类型
4+
5+
- 推荐:使用局部变量,避免污染全局作用域
6+
7+
```lua
8+
local a = 10
9+
local b = true
10+
local s = "hello"
11+
local t = { 1, 2, 3 }
12+
local f = function(x) return x * 2 end
13+
local n = nil
14+
```
15+
16+
- 基本类型:`nil`、`boolean`、`number`、`string`、`table`、`function`
17+
18+
## 2. 控制结构
19+
20+
- if-then-elseif-else
21+
22+
```lua
23+
local x = 5
24+
if x > 10 then
25+
print("big")
26+
elseif x > 5 then
27+
print("medium")
28+
else
29+
print("small")
30+
end
31+
```
32+
33+
- while 循环
34+
35+
```lua
36+
local i = 0
37+
while i < 3 do
38+
print(i)
39+
i = i + 1
40+
end
41+
```
42+
43+
- repeat-until 循环
44+
45+
```lua
46+
local i = 0
47+
repeat
48+
i = i + 1
49+
until i == 3
50+
```
51+
52+
- for 数值型循环
53+
54+
```lua
55+
for i = 1, 5 do
56+
print(i)
57+
end
58+
59+
for i = 10, 1, -2 do
60+
print(i)
61+
end
62+
```
63+
64+
- for 泛型循环(遍历表)
65+
66+
```lua
67+
local arr = { "a", "b", "c" }
68+
for i, v in ipairs(arr) do
69+
print(i, v)
70+
end
71+
72+
local dict = { name = "lua", version = 5.4 }
73+
for k, v in pairs(dict) do
74+
print(k, v)
75+
end
76+
```
77+
78+
## 3. 表(table)操作
79+
80+
- 创建与初始化(数组与字典)
81+
82+
```lua
83+
local arr = { 10, 20, 30 }
84+
local dict = { a = 1, b = 2 }
85+
```
86+
87+
- 插入与删除(数组)
88+
89+
```lua
90+
local arr = { 10 }
91+
table.insert(arr, 20)
92+
table.insert(arr, 1, 5)
93+
local last = table.remove(arr)
94+
local first = table.remove(arr, 1)
95+
```
96+
97+
- 遍历
98+
99+
```lua
100+
local arr = { 10, 20, 30 }
101+
for i, v in ipairs(arr) do
102+
print(i, v)
103+
end
104+
105+
local dict = { a = 1, b = 2 }
106+
for k, v in pairs(dict) do
107+
print(k, v)
108+
end
109+
```
110+
111+
- 删除字典键
112+
113+
```lua
114+
local dict = { a = 1, b = 2 }
115+
dict.a = nil
116+
```
117+
118+
- 数组与字典用法区分
119+
- 数组:顺序索引从 `1` 开始,使用 `ipairs`、`table.insert`、`table.remove`
120+
- 字典:键值对结构,使用 `pairs`,删除通过设为 `nil`
121+
122+
## 4. 函数定义与调用
123+
124+
- 基本定义与调用
125+
126+
```lua
127+
local function add(x, y)
128+
return x + y
129+
end
130+
131+
local r = add(2, 3)
132+
```
133+
134+
- 多返回值
135+
136+
```lua
137+
local function divmod(a, b)
138+
return math.floor(a / b), a % b
139+
end
140+
141+
local q, m = divmod(10, 3)
142+
```
143+
144+
- 可变参数(...)
145+
146+
```lua
147+
local function sum(...)
148+
local total = 0
149+
for i = 1, select("#", ...) do
150+
total = total + select(i, ...)
151+
end
152+
return total
153+
end
154+
155+
local s = sum(1, 2, 3)
156+
```
157+
158+
## 5. 常用标准库函数
159+
160+
- 字符串处理(string)
161+
162+
```lua
163+
local s = "hello lua"
164+
local len = #s
165+
local sub = string.sub(s, 1, 5)
166+
local found = string.find(s, "lua")
167+
local replaced = string.gsub(s, "lua", "world")
168+
local formatted = string.format("%s-%d", "id", 100)
169+
```
170+
171+
- 表操作(table)
172+
173+
```lua
174+
local arr = { "a", "b", "c" }
175+
local joined = table.concat(arr, ",")
176+
table.sort(arr)
177+
```
178+
179+
- 基础 I/O(io)
180+
181+
```lua
182+
for line in io.lines("input.txt") do
183+
print(line)
184+
end
185+
186+
local f = assert(io.open("out.txt", "w"))
187+
f:write("ok\n")
188+
f:close()
189+
```
190+

0 commit comments

Comments
 (0)