[Zhuan]Lua about
Lua 程式開發(fā)筆記
明明我在用 Mac OSX 寫這篇文章,但是依慣例還是要用 FreeBSD 的安裝範(fàn)例
安裝
# cd /usr/ports/lang/lua; make install distclean語法
字串:
> print('aabbcc') aabbcc > print("aabbcc") aabbcc > print([[aabbcc]]) aabbcc > print([=[aabbcc]=]) aabbcc賦值:
> a, b, c, d = true, false, 1, 1 < 2 > print(a, b, c, d) true false 1 true比較:
> print(1 == 1) > print(1 ~= 1) # 注意,不是用 !=nil:
> a, b, c, d = 1, 2 > print(a, b, c, d) 1 2 nil niland, or, not:
> print(1 and 1) 1 > print(nil or 1) 1 > print(not nil) true接續(xù):
> print('Hello' .. 'World') HelloWorld計算長度:
> print(#'abcd') 4 > print(#'Hello, world!') 13 > aa = 'Hello, world!' > print(#aa) 13 > print(string.len(aa)) 13註解:
> -- print('hello') > > --[[ 這是 >> 很長 >> 的註解]] >if:
> if 1 == 1 then print('aa') end aawhile:
> while 1 == 2 do print('should never print') end >for:
> for i = 1, 3 do print(i) end 1 2 3 > for i = 1, 10, 2 do print(i) end 1 3 5 7 9 >repeat(至少執(zhí)行一次):
> repeat print('aa') until 1 < 10 aabreak:
> for a = 1, 10 do >> if a > 5 then >> break >> end >> print(a) >> end 1 2 3 4 5 >type:
> print(type(100)) number > print(type('aa')) string函式
> function echo_aa() >> print('aa') >> end > echo_aa() aalocal variable:
> aa = 'global variable' > function print_aa() >> local aa = 'bb' >> print(aa) >> end > print_aa() bb > print(aa) global variable注意以下兩段:
> -- do block 裡面的 local > aa = 'global variable' > do >> aa = 'bb' >> print(aa) >> end bb > print(aa) global variable > -- 在 global 裡面硬寫 local 是沒有用的 > aa = 'global variable' > local aa = 'bb' > print(aa) global variable取代內(nèi)建函式:
> orig_print = print > function new_print(val) >> orig_print('aa'..val) >> end > print = new_print > print('cc') aacc另種函式寫法:
> print_aa = function() >> print('aa') >> end > print_aa() aalocal 函式:
> aa = function() >> local print = function() print('aa') end >> print('bb') >> end > aa() aaTables
> aa = {['aa']='aa', ['bb']='bb', ['cc'] = 'cc'} > print(aa['bb']) bb > -- 簡易寫法 > new_aa = {aa='aa', bb='bb', cc='cc'} > print(new_aa['aa']) aa > -- 另種取值 > print(new_aa.aa) aa > -- 範(fàn)例 > aa = {bb = {cc = true}} > print(aa.bb.cc) true使用 ipairs 達成 number 為 key 的 table loop:
> aa = {'aa', 'bb', 'cc', 'dd'} > for key, value in ipairs(aa) do >> print(key .. ' -> ' .. value) >> end 1 -> aa 2 -> bb 3 -> cc 4 -> dd使用 pairs 達成 name 為 key 的 table loop:
> aa = {['aa']='aa', ['bb']='bb', ['cc'] = 'cc'} > for key, value in pairs(aa) do >> print(key .. ' -> ' .. value) >> end cc -> cc aa -> aa bb -> bb一些 table 相關(guān)操作:
table.sort(TABLE) table.insert(TABLE, new_item) 等同 TABLE[#TABLE + 1] = new_item table.concat(TABLE) 連接字串 table.remove(TABLE) 移除最後一個 item table.maxn(TABLE) 找出 key 最大正值Tables 的物件導(dǎo)向:
> function aa() >> local function bb() >> return 'bb' >> end >> local function cc() >> return 'cc' >> end >> return {bb = bb, cc = cc} >> end > print(aa().bb()) bb > print(aa().cc()) cc或是:
> aa = {} > function aa:print() >> print('aa') >> end > aa:print() aastrings
一些用法:
string.lower('HELLO') string.upper('hello') string.reverse('hello') string.rep('hello', 2) #=> hellohello string.sub('hello', 1, 4) #=> hell string.len('hello') #=> 5 string.byte('ABCDE', 1, 5) #=> 65 66 67 68 69 string.byte('A') #=> 65 string.char(65) #=> A string.format(">>%15s%6d<<", 'abc', 123) #=> >> abc 123<< string.gsub('The rain in Spain stays mainly in the plain.') #=> The royn in Spoyn stays moynly in the ployn. string.gsub('The rain in Spain stays mainly in the plain.', 999) #=> The royn in Spoyn stays moynly in the ployn. string.gsub('The rain in Spain stays mainly in the plain.', '[ /,.-]', '') #=> TheraininSpainstaysmainlyintheplain string.find('abc', 'b') #=> 2 string.match('abc cde def', '%a+') #=> abcio
io.write('hello') #=> helloFileHnd, ErrStr = io.open('aa.txt', 'w') FileHnd:write('hello') FileHnd:close()os.remove('aa.txt') #=> delete aa.txtio.stdin() io.stdin:read() io.stdout() io.stdout:write() io.stderr() io.lines('aa.txt')command line
取 args 範(fàn)例,將以下存為 select.lua:
#!/usr/bin/env lua print((select('#', ...))) print((select(1, ...))) print((select(2, ...))) print((select(3, ...)))執(zhí)行:
$ lua select.lua a b c 3 a b c另外就是直接 lua 執(zhí)行 string:
$ lua -e 'print("aa")' aamodules
modules 位置(以 unix 為例):
/usr/local/share/lua/5.1 /usr/local/lib/lua/5.1所以:
export LUA_PATH='?.lua;/usr/local/lib/lua/5.1/?.lua'使用 module 的語法為:
require('module_name') require 'module_name'撰寫 module (注意 namespace),如 Util.lua:
Util = {}function Util.Quote(Str)return string.format('%q', Str) endreturn Util為了避免 global variable 與 local 衝突,請使用 strict.lua,會強迫所有 global variable 都先行定義:
require 'strict'local function Test()A = 2B = 3 endA = 1 Test() -- 會產(chǎn)生 assign to undeclared variable 'B' 的錯誤提醒找出 global variable 的方式:
luac -l -p file_name.luamultitask
用法仍須看文件:
coroutine.yield() coroutine.resume() coroutine.wrap() coroutine.create() coroutine.status() coroutine.suspended() coroutine.running() coroutine.normal() coroutine.dead()使用 c lib
其實就是把編譯好的 .so 放到特定目錄中,但詳情還是要看官網(wǎng)
很多參考資料
http://www.lua.org/docs.html
轉(zhuǎn)載于:https://www.cnblogs.com/artstyle/archive/2012/07/24/2605956.html
總結(jié)
以上是生活随笔為你收集整理的[Zhuan]Lua about的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: html5总结2000字,建筑识图实训总
- 下一篇: 大数据量JSONObject.fromO