遍歷Lua全域性環境變數
阿新 • • 發佈:2019-02-18
Lua全域性變數
Lua
直譯器提供了很多全域性變數,比如print
等,便於程式開發。Lua提供的所有全域性變數都儲存在一個普通的表_G
中。目前Lua-5.2.1
中_G中的全域性變數主要有“字串”
、“函式”
及“表”
三種。那該如何遍歷這些值呢?(當然,你在會話中進行的任何變更,都將對其造成影響,除非有local
限定)
如何遍歷Lua提供的全域性變數?
既然_G
是一個普通的表,那麼我們可以採用for
語句對其進行簡單的遍歷即可。具體程式碼,如下:
- for k,v in pairs(_G)do
- print(string.format("%s => %s\n",k,v))
- end
當然,為了遍歷所有的全域性變數(比如,io庫支援的所有函式等),我們需要對所有table
_G._G與_G等效
,所以以下的程式碼中對其進行了過濾,不重複遍歷。並且,此實現僅遍歷了2層)。具體程式碼,如下:
- for k,v in pairs(_G)do
- print(string.format("%s => %s","_G.".. k,v))
- if type(v)=="table"and k ~="_G"then
- for key in pairs(v)do
- print(" ".. k ..".".. key)--為了便於閱讀,進行了特殊格式化處理
- end
- end
- end
示例程式碼最終執行結果,如下:
- $ ./src/lua
- Lua5.2.1Copyright(C)1994-2012
- >
- >for k,v in pairs(_G)do
- >>print(string.format("%s => %s","_G.".. k,v))
- >>if type(v)=="table"and k ~="_G"then
- >>for key in pairs(v)do
- >>print(" ".. k ..".".. key)
- >>end
- >>end
- >>end
- _G.require=>function:0x10da890
- _G.module=>function:0x10da820
- _G.pcall =>function:0x419260
- _G.type =>function:0x418830
- _G.dofile =>function:0x418fd0
- _G.load =>function:0x4195d0
- _G.rawget =>function:0x418c30
- _G.math => table:0x10dd460
- math.frexp
- math.deg
- math.tanh
- math.huge
- math.ceil
- math.acos
- math.pi
- math.asin
- ...
- >
如果只想遍歷os
庫提供的函式列表,可以按以下的方式處理:
- >for k,v in pairs(_G.os)doprint(k)end
- time
- getenv
- remove
- exit
- date
- clock
- tmpname
- difftime
- setlocale
- rename
- execute
比較優雅的實現:基於遞迴的版本
以下是一個基於遞迴的實現版本(為什麼會對package進行特殊處理呢?(提示:死迴圈,可進一步優化處理)):
- function re_print(t,prefix)
- for k,v in pairs(t)do
- if type(v)=="string"then
- print(string.format("%s => %s", prefix ..".".. k,v))
- else
- print(prefix ..".".. k)
- end
- if type(v)=="table"and k ~="_G"and k ~="_G._G"andnot v.packagethen
- re_print(v," ".. prefix ..".".. k)
- end
- end
- end
最終執行結果,如下:
- >function re_print(t,prefix)
- >>for k,v in pairs(t)do
- >>if type(v)=="string"then
- >>print(string.format("%s => %s", prefix ..".".. k,v))
- >>else
- >>print(prefix ..".".. k)
- >>end
- >>if type(v)=="table"and k ~="_G"and k ~="_G._G"andnot v.packagethen
- >> re_print(v," ".. prefix ..".".. k)
- >>end
- >>end
- >>end
- > re_print(_G,"_G")
- _G.next
- _G.bit32
- _G.bit32.rrotate
- _G.bit32.btest
- _G.bit32.extract
- _G.bit32.replace
- _G.bit32.bxor
- _G.bit32.band
- _G.bit32.bnot
- _G.bit32.lrotate
- _G.bit32.arshift
- _G.bit32.rshift
- _G.bit32.lshift
- _G.bit32.bor
- _G._G
- _G.coroutine
- _G.coroutine.running
- _G.coroutine.wrap
- _G.coroutine.create
- _G.coroutine.resume
- _G.coroutine.status
- _G.coroutine.yield
- _G.assert
- _G.type
- _G.print
- _G.module
- _G.debug
- _G.debug.gethook
- _G.debug.traceback
- _G.debug.setuservalue
- _G.debug.setupvalue
- _G.debug.getlocal
- _G.debug.setmetatable
- _G.debug.getuservalue
- _G.debug.setlocal
- _G.debug.getregistry
- _G.debug.sethook
- _G.debug.getupvalue
- _G.debug.upvalueid
- _G.debug.debug
- _G.debug.upvaluejoin
- _G.debug.getmetatable
- _G.debug.getinfo
- _G.rawequal
- _G._VERSION =>Lua5.2
- _G.string
- _G.string.len
- _G.string.find
- _G.string.gsub
- _G.string.gmatch
- _G.string.reverse
- _G.string.byte
- _G.string.format
- _G.string.rep
- _G.string.match
- _G.string.dump
- _G.string.upper
- _G.string.sub
- _G.string.char
- _G.string.lower
- _G.pcall
- _G.table
- _G.table.concat
- _G.table.pack
- _G.table.insert
- _G.table.sort
- _G.table.remove
- _G.table.maxn
- _G.table.unpack
- _G.unpack
- _G.package
- _G.package.seeall
- _G.package.config =>/
- ;
- ?
- !
- -
- _G.package.cpath =>/usr/local/lib/lua/5.2/?.so;/usr/local/lib/lua/5.2/loadall.so;./?.so
- _G.package.searchers
- _G.package<