curl -R -O http://www.lua.org/ftp/lua-5.3.3.tar.gz tar zxf lua-5.3.3.tar.gz cd lua-5.3.3 # aix bsd c89 freebsd generic linux macosx mingw posix solaris make macosx install
Load Lib
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
yume = require('yume') -- load lib then cache & run yume = dofile("lib1.lua") -- reload lib & run yumeFunction = loadfile('yume.lua') -- load lib as function yume = yumeFunction()
--[[ require 'luasocket' no field package.preload['luasocket'] no file './luasocket.lua' no file '/usr/local/share/lua/5.1/luasocket.lua' no file '/usr/local/share/lua/5.1/luasocket/init.lua' no file '/usr/local/lib/lua/5.1/luasocket.lua' no file '/usr/local/lib/lua/5.1/luasocket/init.lua' no file './luasocket.so' no file '/usr/local/lib/lua/5.1/luasocket.so' no file '/usr/local/lib/lua/5.1/loadall.so ]]--
Function
1 2
functionf(x)return x * x end f = function(x)return x * x end
?:
1 2 3 4 5 6
-- a ? b : c a and b or c
-- 解釋 a and b -- b nilor c -- c
for
index
1
for i = startIndex, endIndex, step do doX() end
table as dict
1 2 3 4 5 6 7 8 9
functionpairs(t) returnnext, t, nil end
for k, v innext, t do doX() end
for key, val inpairs(u) do doX() end
table as array
1
for i, v inipairs(a) do doX() end
Iterator
1 2 3 4 5 6 7 8 9 10 11
functionlist_iter(t) local i = 0 local n = table.getn(t) return function() i = i + 1 if i <= n thenreturn t[i] end end end
for element in list_iter(t) do doX() end
1 2 3 4 5 6 7 8 9 10 11 12 13
-- for var_1, ..., var_n in explist do block end for v1,v2,v3,vn in interatorFunction,firstArgumentInitialResult,secondArgumentStartIndex do func() end
functionyume_next(array,index) local _index = index and index + 1or1 local _value = array[_index] ifnot _value thenreturnend return _index,_value end
for i,v in yume_next, {1,2,3}, 1do func() end for i,v in yume_next, {1,2,3}, nildo func() end for i,v in yume_next, {1,2,3} do func() end
特殊呼叫方式
1 2 3 4 5 6
-- 單一 String 參數 print'hello'
-- 單一 Table 參數 functionh(x)print(x.key1) end h{key1 = 'Sonmi~451'} -- Prints 'Sonmi~451'.
全域變數
不需事先宣告,清空方式只需丟入 nil 即可。
1 2 3 4 5
print(b) --> nil b = 10 print(b) --> 10 b = nil print(b) --> nil
localfunctionabcdoprint'x' end return { abc = abc }
Terminal
Before it starts running arguments, lua looks for an environment variable called LUA_INIT. If there is such a variable and its content is @filename, then lua loads the given file. If LUA_INIT is defined but does not start with `@´, then lua assumes that it contains Lua code and runs it. This variable gives you great power when configuring the stand-alone interpreter, because you have the full power of Lua in the configuration. You can pre-load packages, change the prompt and the path, define your own functions, rename or delete functions, and so on.
1 2 3 4 5
prompt> lua -e"print(math.sin(12))" --> -0.53657291800043 # prompt> lua -i -l a.lua -e "x = 10" prompt> lua -i -e"_PROMPT=' lua> '" # prompt> lua script a b c # prompt> lua -e "sin=math.sin" script a b
days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"} days[1] == "Sunday"-- 陣列從 1 開始 w = {x=0, y=0, label="console"}
1 2 3 4 5 6 7 8 9 10
print(type("Hello world")) --> string print(type(10.4*3)) --> number
print(type(a)) --> nil (`a' is not initialized) a = 10 print(type(a)) --> number a = "a string!!" print(type(a)) --> string a = print-- yes, this is valid! a(type(a)) --> function
Metatable & Metafunction
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
defaultFavs = {animal = 'gru', food = 'donuts'} myFavs = {food = 'pizza'} setmetatable(myFavs, {__index = defaultFavs}) eatenBy = myFavs.animal -- __add(a, b) for a + b -- __sub(a, b) for a - b -- __mul(a, b) for a * b -- __div(a, b) for a / b -- __mod(a, b) for a % b -- __pow(a, b) for a ^ b -- __unm(a) for -a -- __concat(a, b) for a .. b -- __len(a) for #a -- __eq(a, b) for a == b -- __lt(a, b) for a < b -- __le(a, b) for a <= b -- __index(a, b) <fn or a table> for a.b -- __newindex(a, b, c) for a.b = c -- __call(a, ...) for a(...)
wget http://luarocks.github.io/luarocks/releases/luarocks-2.4.1.tar.gz tar zxvf luarocks-2.4.1.tar.gz cd luarocks-2.4.1 ./configure make build make install