10分钟学会Lua
Lua是一个简单轻量的脚本语言,例子来自Lua官方的小例子,全部学会就会用啦
– Example 1 – Helloworld
用print就可以打印啦
|
|
– Example 2 – 注释
注释有两种啦,两个横线,多行注释要用两个方括号
|
|
– Example 3 – 变量.
变量不用声明哒,直接用,而且没有类型 hhh 类似于py叭
|
|
– Example 4 – 变量名.
注:和C语言一样
|
|
变量名可以用 字母 数字 and 下划线
但是不可以使用数字开头
– Example 5 保留字.
就是下划线开头的
|
|
下划线是保留字使用的开头,变量不能使用_开头
– Example 6 – 区分大小写.
不用解释
|
|
区分大小写
– Example 7 – 保留的关键字.
大写的不是关键字
|
|
– Example 8 – 字符串
3种, 单引号、双引号和多行(多行包含换行)
|
|
3种字符串的表示方法
– Example 9 – 奇怪的赋值方式是支持的
|
|
– Example 10 – 奇怪的赋值方式可以交换变量
|
|
– Example 11 – 数字
用两个点可以连接字符串和数字
|
|
– Example 12 – print可以不写括号?
|
|
– Example 13 – 可以用stdout嗷
|
|
io.write可以不换行 空的print可以输出一个新行
– Example 14 – 数组.
数组,官方文档写的是Tables,可以用下标来访问
|
|
– Example 15 下标可以是字符串
|
|
– Example 16 – if statement.
记得有then和end
|
|
– Example 17 – if else statement.
if else end
|
|
if then else end
– Example 18 – if elseif else statement
多分支
|
|
if then elseif then else end
– Example 19 – 条件赋值?
有点像C的三目运算符
相当于b = ( a == 1) ? "one": "not one";
|
|
– Example 20 – while
while do end (~=就是C语言的!=)
|
|
– Example 21 – repeat until statement.
do while?
|
|
– Example 22 – for循环
for [1,4] do end
|
|
for do end
– Example 23 – foreach?.
for还能这样用*(foreach?)
|
|
– Example 24 – 用pairs打印数组.
pairs?
|
|
– Example 25 – break跳出循环
|
|
– Example 26 – 函数.
|
|
– Example 27 – 带返回值的函数
|
|
– Example 28 – 返回一堆值
|
|
– Example 29 –局部变量 local
默认变量就是全局变量,局部变量要用local声明
|
|
All variables are global in scope by default.
– Example 30 – 格式化输出printf
|
|
– Example 31 标准库?
|
|
– Example 32 – math库.
|
|
– Example 33 – string库.
|
|
– Example 34 – table库.
|
|
– Example 35 –input/output库.
|
|
– Example 36 – os库
|
|
– Example 37 – 外部库
外部的库需要用require导入
|
|
– Example 38 关于例子
|
|
- 原文作者:xuehu
- 原文链接:https://www.xuehu96.com/post/Lua/
- 版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议进行许可,非商业转载请注明出处(作者,原文链接),商业转载请联系作者获得授权。