Lua 入门
示例
变数
var = 50 -- a global variable print(var) --> 50 do local var = 100 -- a local variable print(var) --> 100 end print(var) --> 50 -- The global var (50) still exists -- The local var (100) has gone out of scope and can't be accessed any longer.
类型
num = 20 -- a number
num = 20.001 -- still a number
str = "zaldrizes buzdari iksos daor" -- a string
tab = {1, 2, 3} -- a table (these have their own category)
bool = true -- a boolean value
bool = false -- the only other boolean value
print(type(num)) --> 'number'
print(type(str)) --> 'string'
print(type(bool)) --> 'boolean'
type(type(num)) --> 'string'
-- Functions are a type too, and first-class values in Lua.
print(type(print)) --> prints 'function'
old_print = print
print = function (x) old_print "I'm ignoring the param you passed me!" end
old_print(type(print)) --> Still prints 'function' since it's still a function.
-- But we've (unhelpfully) redefined the behavior of print.
print("Hello, world!") --> prints "I'm ignoring the param you passed me!"特殊类型nil
Lua中的另一种类型是nil。该nil类型中的唯一值是nil。nil存在与Lua中的所有其他值不同。这是一种非价值的价值。
print(foo) -- This prints nil since there's nothing stored in the variable 'foo'. foo = 20 print(foo) -- Now this prints 20 since we've assigned 'foo' a value of 20. -- We can also use `nil` to undefine a variable foo = nil -- Here we set 'foo' to nil so that it can be garbage-collected. if nil 然后打印 "nil" end --> (prints nothing) -- Only false and nil are considered false; every other value is true. if 0 然后打印 "0" end --> 0 if "" 然后打印 "Empty string!" --> Empty string!
表达
a = 3
b = a + 20 a = 2 print(b, a) -- hard to read, can also be written as
b = a + 20; a = 2; print(a, b) -- easier to read, ; are optional though
true and true --> returns true
true and 20 --> 20
false and 20 --> false
false or 20 --> 20
true or 20 --> true
tab or {}
--> returns tab if it is defined
--> returns {} if tab is undefined
-- This is useful when we don't know if a variable exists
tab = tab or {} -- tab stays unchanged if it exists; tab becomes {} if it was previously nil.
a, b = 20, 30 -- this also works
a, b = b, a -- switches values定义功能
function name(parameter)
return parameter
end
print(name(20)) --> 20
-- see function category for more information
name = function(parameter) return parameter end -- Same as above布尔值
仅false并将其nil评估为false,其他所有内容(包括0和空字符串)均评估为true。
垃圾收集
tab = {"lots", "of", "data"}
tab = nil; collectgarbage()
-- tab does no longer exist, and doesn't take up memory anymore.桌子
tab1 = {"a", "b", "c"}
tab2 = tab1
tab2[1] = "d"
print(tab1[1]) --> 'd' -- table values only store references.
--> assigning tables does not copy its content, only the reference.
tab2 = nil; collectgarbage()
print(tab1) --> (prints table address) -- tab1 still exists; it didn't get garbage-collected.
tab1 = nil; collectgarbage()
-- No more references. Now it should actually be gone from memory.这些是基础知识,但是其中有关于表的部分,其中包含更多信息。
条件
if (condition) then
-- do something
elseif (other_condition) then
-- do something else
else
-- do something
endfor循环
forLua中有两种循环类型:数字for循环和通用for循环。
数字for循环具有以下形式:
fora=1,10,2do--forastartingat1,endingat10,instepsof2
print(a)-->1,3,5,7,9
end
数字for循环中的第三个表达式是循环递增的步骤。这样可以很容易地进行反向循环:
fora=10,1,-1do
print(a)-->10,9,8,7,6,etc.
end
如果省略了步骤表达式,则Lua会采用默认步骤1。
fora=1,10do
print(a)-->1,2,3,4,5,etc.
end
还要注意,循环变量是循环的局部变量for。循环结束后将不存在。
通用for循环遍历迭代器函数返回的所有值:
forkey,valueinpairs({"some","table"})do
print(key,value)
-->1some
-->2table
end
Lua中提供了一些内置的迭代器(例如pairs,ipairs),用户可以定义自己的自定义迭代器,以及与一般使用for的循环。
做块
local a = 10
do
print(a) --> 10
local a = 20
print(a) --> 20
end
print(a) --> 10