Lua是一个简单轻量的脚本语言,例子来自Lua官方的小例子,全部学会就会用啦
– Example 1 – Helloworld
用print就可以打印啦
1 | lua复制代码-- Classic hello program. |
– Example 2 – 注释
注释有两种啦,两个横线,多行注释要用两个方括号
1 | lua复制代码-- Single line comments in Lua start with double hyphen. |
– Example 3 – 变量.
变量不用声明哒,直接用,而且没有类型 hhh 类似于py叭
1 | lua复制代码-- Variables hold values which have types, variables don't have types. |
– Example 4 – 变量名.
注:和C语言一样
1 | lua复制代码-- Variable names consist of letters, digits and underscores. |
变量名可以用 字母 数字 and 下划线
但是不可以使用数字开头
– Example 5 保留字.
就是下划线开头的
1 | lua复制代码-- The underscore is typically used to start special values |
下划线是保留字使用的开头,变量不能使用_开头
– Example 6 – 区分大小写.
不用解释
1 | lua复制代码-- Lua is case sensitive so all variable names & keywords |
区分大小写
– Example 7 – 保留的关键字.
大写的不是关键字
1 | lua复制代码-- Lua reserved words are: and, break, do, else, elseif, |
– Example 8 – 字符串
3种, 单引号、双引号和多行(多行包含换行)
1 | lua复制代码a="single 'quoted' string and double \"quoted\" string inside" |
3种字符串的表示方法
– Example 9 – 奇怪的赋值方式是支持的
1 | lua复制代码-- a,b = b,a is valid |
– Example 10 – 奇怪的赋值方式可以交换变量
1 | lua复制代码-- Multiple assignments allows one line to swap two variables. |
– Example 11 – 数字
用两个点可以连接字符串和数字
1 | lua复制代码-- Multiple assignment showing different number formats. |
– Example 12 – print可以不写括号?
1 | lua复制代码-- More writing output. |
– Example 13 – 可以用stdout嗷
1 | lua复制代码-- io.write writes to stdout but without new line. |
io.write可以不换行 空的print可以输出一个新行
– Example 14 – 数组.
数组,官方文档写的是Tables,可以用下标来访问
1 | lua复制代码-- Simple table creation. |
– Example 15 下标可以是字符串
1 | lua复制代码-- Associate index style. |
– Example 16 – if statement.
记得有then和end
1 | lua复制代码-- Simple if. |
– Example 17 – if else statement.
if else end
1 | lua复制代码b="happy" |
if then else end
– Example 18 – if elseif else statement
多分支
1 | lua复制代码c=3 |
if then elseif then else end
– Example 19 – 条件赋值?
有点像C的三目运算符
相当于b = ( a == 1) ? "one": "not one";
1 | lua复制代码-- value = test and x or y |
– Example 20 – while
while do end (~=就是C语言的!=)
1 | lua复制代码a=1 |
– Example 21 – repeat until statement.
do while?
1 | lua复制代码a=0 |
– Example 22 – for循环
for [1,4] do end
1 | lua复制代码-- Numeric iteration form. |
for do end
– Example 23 – foreach?.
for还能这样用*(foreach?)
1 | lua复制代码-- Sequential iteration form. |
– Example 24 – 用pairs打印数组.
pairs?
1 | lua复制代码-- Simple way to print tables. ## iterator |
– Example 25 – break跳出循环
1 | lua复制代码-- break is used to exit a loop. |
– Example 26 – 函数.
1 | lua复制代码-- Define a function without parameters or return value. |
– Example 27 – 带返回值的函数
1 | lua复制代码-- Define a function with a return value. |
– Example 28 – 返回一堆值
1 | lua复制代码-- Define function with multiple parameters and multiple return values. |
– Example 29 –局部变量 local
默认变量就是全局变量,局部变量要用local声明
1 | lua复制代码-- All variables are global in scope by default. |
All variables are global in scope by default.
– Example 30 – 格式化输出printf
1 | lua复制代码-- An implementation of printf. |
– Example 31 标准库?
1 | lua复制代码--[[ |
– Example 32 – math库.
1 | lua复制代码-- Math functions: |
– Example 33 – string库.
1 | lua复制代码-- String functions: |
– Example 34 – table库.
1 | lua复制代码-- Table functions: |
– Example 35 –input/output库.
1 | lua复制代码-- IO functions: |
– Example 36 – os库
1 | lua复制代码-- OS functions: |
– Example 37 – 外部库
外部的库需要用require导入
1 | lua复制代码-- Lua has support for external modules using the 'require' function |
– Example 38 关于例子
1 | lua复制代码--[[ |
本文转载自: 掘金