mirror of
https://github.com/jaywcjlove/reference.git
synced 2025-06-16 20:21:22 +08:00
@ -26,7 +26,7 @@ Quick Reference
|
||||
[FFmpeg](./docs/ffmpeg.md)<!--rehype:style=background: rgb(0 193 9/var(\-\-bg\-opacity));&class=contributing-->
|
||||
[LaTeX](./docs/latex.md)<!--rehype:style=background: rgb(0 128 128/var(\-\-bg\-opacity));&class=contributing-->
|
||||
[MATLAB](./docs/matlab.md)<!--rehype:style=background: rgb(0 118 168/var(\-\-bg\-opacity));&class=contributing-->
|
||||
[Vue 3 ](./docs/vue.md)<!--rehype:style=background: rgb(64 184 131/var(\-\-bg\-opacity));&class=contributing-->
|
||||
[Vue 3](./docs/vue.md)<!--rehype:style=background: rgb(64 184 131/var(\-\-bg\-opacity));&class=contributing-->
|
||||
<!--rehype:class=home-card-->
|
||||
|
||||
## 编程
|
||||
@ -59,6 +59,7 @@ Quick Reference
|
||||
[SwiftUI](./docs/swiftui.md)<!--rehype:style=background: rgb(10 127 247/var(\-\-bg\-opacity));&class=tag&data-lang=swift-->
|
||||
[TOML](./docs/toml.md)<!--rehype:style=background: rgb(132 132 132/var(\-\-bg\-opacity));-->
|
||||
[YAML](./docs/yaml.md)<!--rehype:style=background: rgb(91 163 230/var(\-\-bg\-opacity));-->
|
||||
[Lua](./docs/lua.md)<!--rehype:style=background: rgb(64 196 255/var(\-\-bg\-opacity));-->
|
||||
<!--rehype:class=home-card-->
|
||||
|
||||
## 前端
|
||||
@ -77,7 +78,7 @@ Quick Reference
|
||||
[RegEx 正则表达式](./docs/regex.md)<!--rehype:style=background: rgb(149 36 155/var(\-\-bg\-opacity));-->
|
||||
[TypeScript](./docs/typescript.md)<!--rehype:style=background: rgb(49 120 198/var(\-\-bg\-opacity));-->
|
||||
[Vue 2](./docs/vue2.md)<!--rehype:style=background: rgb(64 184 131/var(\-\-bg\-opacity));-->
|
||||
[Vue 3 ](./docs/vue.md)<!--rehype:style=background: rgb(64 184 131/var(\-\-bg\-opacity));&class=contributing-->
|
||||
[Vue 3](./docs/vue.md)<!--rehype:style=background: rgb(64 184 131/var(\-\-bg\-opacity));&class=contributing-->
|
||||
<!--rehype:class=home-card-->
|
||||
|
||||
## Nodejs
|
||||
@ -292,7 +293,6 @@ Quick Reference
|
||||
|
||||
如果你有资源,可以很方便部署 web 版,这非常简单,只需要克隆 gh-pages 分支代码到你的静态服务就可以了,还可以使用 [docker](https://hub.docker.com/r/wcjiang/reference) 快捷部署 web 版。
|
||||
|
||||
|
||||
<!--rehype:ignore:start-->
|
||||
## License
|
||||
|
||||
|
437
docs/lua.md
Normal file
437
docs/lua.md
Normal file
@ -0,0 +1,437 @@
|
||||
Lua 备忘清单
|
||||
===
|
||||
|
||||
包含最重要概念、函数、方法等的 Lua 备忘单。 初学者的完整快速参考。
|
||||
|
||||
入门
|
||||
---
|
||||
|
||||
### 下载
|
||||
|
||||
macos 使用 homebrew 下载
|
||||
|
||||
```bash
|
||||
$ brew install lua
|
||||
```
|
||||
|
||||
#### 其它下载方式
|
||||
|
||||
* [下载地址](https://luabinaries.sourceforge.net/download.html)
|
||||
|
||||
```bash
|
||||
# 查看 lua 是否安装成功
|
||||
$ lua -v
|
||||
```
|
||||
|
||||
### hello world
|
||||
|
||||
```lua
|
||||
#!/usr/bin/env lua
|
||||
|
||||
print("Hello World!")
|
||||
```
|
||||
|
||||
#### 运行
|
||||
|
||||
```bash
|
||||
$ lua ./hello.lua
|
||||
# 或者也可以像 bash 脚本一样
|
||||
$ chmod +x hello.lua
|
||||
./hello.lua
|
||||
```
|
||||
|
||||
### 注释
|
||||
|
||||
#### 单行注释
|
||||
|
||||
```lua
|
||||
-- 以两个减号开始
|
||||
```
|
||||
|
||||
#### 多行注释
|
||||
|
||||
多行注释以 `--[[` 开头, 以 `]]` 结尾
|
||||
|
||||
```lua
|
||||
--[[
|
||||
|
||||
]]
|
||||
```
|
||||
<!--rehype:className=wrap-text -->
|
||||
|
||||
### type() 函数
|
||||
|
||||
使用 `type()` 函数可以判断变量或者值的类型
|
||||
|
||||
```lua
|
||||
print(type(true)) -- boolean
|
||||
print(type(nil)) -- nil
|
||||
```
|
||||
|
||||
### number
|
||||
|
||||
Lua 默认只有一种 number 类型 double (双精度) 类型
|
||||
|
||||
```lua
|
||||
print(10)
|
||||
print(0.3)
|
||||
print(2e + 10)
|
||||
```
|
||||
|
||||
### string
|
||||
<!--rehype:wrap-class=row-span-2-->
|
||||
|
||||
```lua
|
||||
-- 使用 ''
|
||||
local str1 = 'str1'
|
||||
-- 使用 ""
|
||||
local str2 = "str2"
|
||||
```
|
||||
|
||||
#### `[[]]`
|
||||
|
||||
使用 `[[]]` 跨行表示多个字符串
|
||||
|
||||
```lua
|
||||
local html = [[
|
||||
<html>
|
||||
<head></head>
|
||||
<body>
|
||||
<a href="https://www.twle.cn/">简单编程</a>
|
||||
</body>
|
||||
</html>
|
||||
]]
|
||||
print(html)
|
||||
```
|
||||
|
||||
#### 字符串连接(`..`)
|
||||
|
||||
```lua
|
||||
print("a" .. 'b')
|
||||
-- ab
|
||||
print(157 .. 428)
|
||||
-- 157428
|
||||
```
|
||||
|
||||
#### 字符串长度(`#`)
|
||||
|
||||
```lua
|
||||
print(#"string") -- 6
|
||||
```
|
||||
|
||||
### table
|
||||
|
||||
```lua
|
||||
local table = {}
|
||||
```
|
||||
|
||||
#### 迭代 table
|
||||
|
||||
默认的初始索引会从 1 开始
|
||||
|
||||
```lua
|
||||
local array = { "apple", "pear", "orange", "grape" }
|
||||
|
||||
print(array[1]) -- apple
|
||||
|
||||
for k, v in pairs(array) do
|
||||
print(k .. " : " .. v)
|
||||
end
|
||||
-- 1 : apple
|
||||
-- 2 : pear
|
||||
-- 3 : orange
|
||||
-- 4 : grape
|
||||
```
|
||||
|
||||
#### 指定键
|
||||
|
||||
```lua
|
||||
local array = {}
|
||||
array.one = "apple"
|
||||
array["two"] = "peach"
|
||||
|
||||
print(array.one) -- apple
|
||||
print(array.two) -- peach
|
||||
```
|
||||
|
||||
### 变量
|
||||
|
||||
#### 默认值
|
||||
|
||||
变量的默认值均是 nil
|
||||
|
||||
```lua
|
||||
#!/usr/bin/env lua
|
||||
print(b) -- nil
|
||||
```
|
||||
|
||||
#### 全局和局部变量
|
||||
|
||||
Lua 中的变量全是全局变量,那怕是语句块或是函数里,除非用 local 显式声明为局部变量
|
||||
|
||||
```lua
|
||||
#!/usr/bin/env lua
|
||||
function main()
|
||||
local b = 12
|
||||
a = 23
|
||||
end
|
||||
|
||||
main()
|
||||
print(a) -- 23
|
||||
print(b) -- nil
|
||||
```
|
||||
|
||||
<!--rehype:className=wrap-text -->
|
||||
|
||||
### 赋值
|
||||
<!--rehype:wrap-class=row-span-2-->
|
||||
|
||||
```lua
|
||||
a = "hello " .. "world" -- 改变 变量
|
||||
t.n = t.n + 1 -- 改变 table
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
```lua
|
||||
-- 给多个变量赋值
|
||||
a, b = 10, 2*a --> a=10; b=20
|
||||
```
|
||||
|
||||
#### 交换变量
|
||||
|
||||
```lua
|
||||
local x, y = 1, 3
|
||||
x, y = y, x
|
||||
|
||||
print(x, y) -- 3, 1
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
```lua
|
||||
local tab = {}
|
||||
tab.one = 2
|
||||
tab.two = 1
|
||||
|
||||
tab["one"], tab["two"] = tab.two, tab.one
|
||||
|
||||
print(tab.one, tab.two) -- 1 2
|
||||
```
|
||||
|
||||
#### 赋值个数不一致
|
||||
|
||||
* 如果变量个数**大于**值的个数,按变量个数补足 nil
|
||||
|
||||
```lua
|
||||
a, b, c = 1, 3
|
||||
print(a,b,c) --> 1 3 nil
|
||||
```
|
||||
|
||||
* 如果变量个数**小于**值的个数,多余的值会被忽略
|
||||
|
||||
```lua
|
||||
a = 1
|
||||
local a, b = a, a + 1, a + 2
|
||||
print(a, b) --> 1 2
|
||||
```
|
||||
|
||||
<!--rehype:className=style-round-->
|
||||
<!--rehype:className=wrap-text -->
|
||||
|
||||
### 运算符
|
||||
|
||||
:- | :-
|
||||
:- | :-
|
||||
\+ | 加法
|
||||
\- | 减法
|
||||
\* | 乘法
|
||||
\/ | 除法
|
||||
\% | 取余,求出除法的余数
|
||||
\^ | 乘幂,计算次方
|
||||
\- | 负号,取负值
|
||||
|
||||
```lua
|
||||
local a, b = 4, 3
|
||||
|
||||
print(a + b) -- 7
|
||||
print(a - b) -- 1
|
||||
print(a / b) -- 1.3333333333333
|
||||
print(a * b) -- 12
|
||||
print(a % b) -- 1
|
||||
print(a ^ b) -- 64.0
|
||||
```
|
||||
|
||||
### 类型转换
|
||||
|
||||
* 在算术运算中,string 类型会尝试自动转换为 number 时
|
||||
|
||||
```lua
|
||||
local a, b, c = "str", "1", "2"
|
||||
|
||||
-- print(a + b) -- error
|
||||
print(b + c) -- 3
|
||||
```
|
||||
|
||||
* number 类型使用 `..` 会自动转换为 string
|
||||
|
||||
```lua
|
||||
local a, b = 1, 2
|
||||
print(type(a .. b))
|
||||
```
|
||||
|
||||
* 其它方式的转换
|
||||
|
||||
```lua
|
||||
print(type(tostring(12))) -- string
|
||||
print(type(tonumber("12"))) -- number
|
||||
```
|
||||
<!--rehype:className=style-round-->
|
||||
<!--rehype:className=wrap-text -->
|
||||
|
||||
条件语句
|
||||
---
|
||||
|
||||
### 运算符
|
||||
<!--rehype:wrap-class=row-span-2-->
|
||||
|
||||
#### 关系运算符
|
||||
|
||||
符号 | 含义
|
||||
:- | :-
|
||||
== | 等于
|
||||
~= | 不等于
|
||||
\> | 大于
|
||||
\< | 小于
|
||||
\>= | 大于等于
|
||||
\<= | 小于等于
|
||||
<!--rehype:className=show-header-->
|
||||
|
||||
```lua
|
||||
local a, b = 4, 3
|
||||
|
||||
print(a < b) -- false
|
||||
print(a <= b) -- false
|
||||
print(a == b) -- false
|
||||
print(a ~= b) -- true
|
||||
print(a > b) -- true
|
||||
print(a >= b)-- true
|
||||
```
|
||||
|
||||
#### 逻辑运算符
|
||||
|
||||
符号 | 含义
|
||||
:- | :-
|
||||
`and` | 逻辑与
|
||||
`or` | 逻辑或操作符
|
||||
`not` | 逻辑非操作符
|
||||
<!--rehype:className=show-header-->
|
||||
|
||||
```lua
|
||||
local a, b = true, false
|
||||
print(a and b) -- false
|
||||
print(a and not b) -- true
|
||||
print(a or b) -- true
|
||||
```
|
||||
|
||||
### while 循环
|
||||
|
||||
```lua
|
||||
local num = 1
|
||||
while (num < 5) do
|
||||
print("num 的值为:", num)
|
||||
num = num + 1
|
||||
end
|
||||
```
|
||||
|
||||
### if 语句
|
||||
<!--rehype:wrap-class=row-span-2-->
|
||||
|
||||
<span style="color:red">注意: Lua 中 0 为 true,但是 Lua 中的 `nil` 可以当作 `false`</span>
|
||||
|
||||
```lua
|
||||
if(0)
|
||||
then
|
||||
print("0 为 true")
|
||||
end
|
||||
```
|
||||
|
||||
#### if .. elseif() .. else
|
||||
|
||||
```lua
|
||||
local age = 27;
|
||||
|
||||
if (age < 18)
|
||||
then
|
||||
print("age 小于 18")
|
||||
elseif (age < 25)
|
||||
then
|
||||
print("age 小于 25")
|
||||
elseif (age < 30)
|
||||
then
|
||||
print("age 小于 30")
|
||||
else
|
||||
print("age 大于 30")
|
||||
end
|
||||
|
||||
print("age 的值为 :", age)
|
||||
```
|
||||
|
||||
### for 循环
|
||||
|
||||
```lua
|
||||
for i = 10, 1, -1 do
|
||||
print(i)
|
||||
end
|
||||
```
|
||||
|
||||
* lua 中的 for 循环从参数 1 变化到参数 2,每次变化以参数 3 为步长递增 i,并执行一次表达式
|
||||
* 参数三,是可选的,如果不指定,默认是 1
|
||||
* 参数二只会在一开始求值,其后不会再进行运算
|
||||
|
||||
```lua
|
||||
local f = function(x)
|
||||
print("in f(x) ")
|
||||
return x * 2
|
||||
end
|
||||
|
||||
for i = 1, f(5) do
|
||||
print(i)
|
||||
end
|
||||
```
|
||||
|
||||
<!--rehype:className=style-round-->
|
||||
<!--rehype:className=wrap-text -->
|
||||
|
||||
### repeat...until 循环
|
||||
|
||||
repeat...until 循环的条件语句在当前循环结束后判断
|
||||
|
||||
```lua
|
||||
local num = 11
|
||||
repeat
|
||||
print("num 的值为: ", num)
|
||||
num = num + 1
|
||||
until (num > 10)
|
||||
-- num 的值为:11
|
||||
```
|
||||
|
||||
### break
|
||||
|
||||
```lua
|
||||
local num = 11
|
||||
repeat
|
||||
print("num 的值为: ", num)
|
||||
num = num + 1
|
||||
if (num > 15) then
|
||||
break
|
||||
end
|
||||
until (num > 20)
|
||||
```
|
||||
|
||||
另见
|
||||
----
|
||||
|
||||
* [Lua](http://www.lua.org)
|
||||
* [luatos](https://wiki.luatos.com/luaGuide/introduction.html)
|
||||
* [Lua 教程](https://www.twle.cn/l/yufei/lua53/lua-basic-index.html)
|
27
scripts/assets/lua.svg
Normal file
27
scripts/assets/lua.svg
Normal file
@ -0,0 +1,27 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- Generator: Adobe Illustrator 14.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 43363) -->
|
||||
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
|
||||
<svg version="1.0" id="Ebene_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
|
||||
width="1em" height="1em" viewBox="0 0 947 947" enable-background="new 0 0 947 947" xml:space="preserve">
|
||||
<g>
|
||||
<path fill="#000080" d="M835.5,473.6c0-199.8-162.2-362-362-362s-362,162.2-362,362c0,199.8,162.2,362,362,362
|
||||
S835.5,673.4,835.5,473.6"/>
|
||||
<path fill="#FFFFFF" d="M729.5,323.6c0-58.5-47.5-106-106-106s-106,47.5-106,106c0,58.5,47.5,106,106,106S729.5,382.1,729.5,323.6"
|
||||
/>
|
||||
<path fill="#000080" d="M941.5,111.5c0-58.5-47.5-106-106-106s-106,47.5-106,106c0,58.5,47.5,106,106,106S941.5,170.1,941.5,111.5"
|
||||
/>
|
||||
<g>
|
||||
<path fill="#FFFFFF" d="M258.1,627.8h117.3v26.7H227.8V417h30.3V627.8z"/>
|
||||
<path fill="#FFFFFF" d="M515.5,654.5v-23.8c-16,22.5-31.9,31.3-57,31.3c-33.2,0-54.4-18.2-54.4-46.6V483.8h27v120.9
|
||||
c0,20.5,13.7,33.6,35.2,33.6c28.3,0,46.6-22.8,46.6-57.7v-96.8h27v170.7H515.5z"/>
|
||||
<path fill="#FFFFFF" d="M738.4,659.1c-8.8,2.3-13,2.9-18.6,2.9c-17.6,0-26.1-7.8-28-25.1c-19.2,17.6-36.5,25.1-58,25.1
|
||||
c-34.5,0-56-19.5-56-50.5c0-22.2,10.1-37.5,30-45.6c10.4-4.2,16.3-5.5,54.7-10.4c21.5-2.6,28.3-7.5,28.3-18.9v-7.2
|
||||
c0-16.3-13.7-25.4-38.1-25.4c-25.4,0-37.8,9.4-40.1,30.3h-27.4c0.7-16.9,3.9-26.7,11.7-35.5c11.4-12.7,31.9-19.9,56.7-19.9
|
||||
c42,0,64.2,16.3,64.2,46.6v100.4c0,8.5,5.2,13.4,14.7,13.4c1.6,0,2.9,0,5.9-0.7V659.1z M690.8,570.1c-9.1,4.2-15,5.5-43.7,9.4
|
||||
c-29,4.2-41.1,13.4-41.1,31.3c0,17.3,12.4,27.4,33.6,27.4c16,0,29.3-5.2,40.4-15.3c8.1-7.5,10.8-13,10.8-22.2V570.1z"/>
|
||||
</g>
|
||||
<path fill="none" stroke="#808080" stroke-width="10.8612" stroke-miterlimit="10" stroke-dasharray="40.8475" d="M890.6,261
|
||||
c33.5,65.8,51,138.6,51,212.5c0,258.4-209.7,468.1-468.1,468.1S5.4,731.9,5.4,473.5C5.4,215.1,215.1,5.4,473.5,5.4
|
||||
c83.1,0,164.6,22.1,236.2,63.9"/>
|
||||
</g>
|
||||
</svg>
|
After Width: | Height: | Size: 2.0 KiB |
Reference in New Issue
Block a user