1. 程式人生 > >遍歷Lua全域性環境變數

遍歷Lua全域性環境變數

Lua全域性變數

Lua直譯器提供了很多全域性變數,比如print等,便於程式開發。Lua提供的所有全域性變數都儲存在一個普通的表_G中。目前Lua-5.2.1中_G中的全域性變數主要有“字串”“函式”“表”三種。那該如何遍歷這些值呢?(當然,你在會話中進行的任何變更,都將對其造成影響,除非有local限定)

如何遍歷Lua提供的全域性變數?

既然_G是一個普通的表,那麼我們可以採用for語句對其進行簡單的遍歷即可。具體程式碼,如下:

  1. for k,v in pairs(_G)do
  2. print(string.format("%s => %s\n",k,v))
  3. end

當然,為了遍歷所有的全域性變數(比如,io庫支援的所有函式等),我們需要對所有table

類變數進行遍歷(由於_G._G與_G等效,所以以下的程式碼中對其進行了過濾,不重複遍歷。並且,此實現僅遍歷了2層)。具體程式碼,如下:

  1. for k,v in pairs(_G)do
  2. print(string.format("%s => %s","_G.".. k,v))
  3. if type(v)=="table"and k ~="_G"then
  4. for key in pairs(v)do
  5. print(" ".. k ..".".. key)--為了便於閱讀,進行了特殊格式化處理
  6. end
  7. end
  8. end

示例程式碼最終執行結果,如下:

  1. $ ./src/lua
  2. Lua5.2.1Copyright(C)1994-2012
    Lua.org, PUC-Rio
  3. >
  4. >for k,v in pairs(_G)do
  5. >>print(string.format("%s => %s","_G.".. k,v))
  6. >>if type(v)=="table"and k ~="_G"then
  7. >>for key in pairs(v)do
  8. >>print(" ".. k ..".".. key)
  9. >>end
  10. >>end
  11. >>end
  12. _G.require=>function:0x10da890
  13. _G.module=>function:0x10da820
  14. _G.pcall =>function:0x419260
  15. _G.type =>function:0x418830
  16. _G.dofile =>function:0x418fd0
  17. _G.load =>function:0x4195d0
  18. _G.rawget =>function:0x418c30
  19. _G.math => table:0x10dd460
  20. math.frexp
  21. math.deg
  22. math.tanh
  23. math.huge
  24. math.ceil
  25. math.acos
  26. math.pi
  27. math.asin
  28. ...
  29. >

如果只想遍歷os庫提供的函式列表,可以按以下的方式處理:

  1. >for k,v in pairs(_G.os)doprint(k)end
  2. time
  3. getenv
  4. remove
  5. exit
  6. date
  7. clock
  8. tmpname
  9. difftime
  10. setlocale
  11. rename
  12. execute

比較優雅的實現:基於遞迴的版本

以下是一個基於遞迴的實現版本(為什麼會對package進行特殊處理呢?(提示:死迴圈,可進一步優化處理)):

  1. function re_print(t,prefix)
  2. for k,v in pairs(t)do
  3. if type(v)=="string"then
  4. print(string.format("%s => %s", prefix ..".".. k,v))
  5. else
  6. print(prefix ..".".. k)
  7. end
  8. if type(v)=="table"and k ~="_G"and k ~="_G._G"andnot v.packagethen
  9. re_print(v," ".. prefix ..".".. k)
  10. end
  11. end
  12. end

最終執行結果,如下:

  1. >function re_print(t,prefix)
  2. >>for k,v in pairs(t)do
  3. >>if type(v)=="string"then
  4. >>print(string.format("%s => %s", prefix ..".".. k,v))
  5. >>else
  6. >>print(prefix ..".".. k)
  7. >>end
  8. >>if type(v)=="table"and k ~="_G"and k ~="_G._G"andnot v.packagethen
  9. >> re_print(v," ".. prefix ..".".. k)
  10. >>end
  11. >>end
  12. >>end
  13. > re_print(_G,"_G")
  14. _G.next
  15. _G.bit32
  16. _G.bit32.rrotate
  17. _G.bit32.btest
  18. _G.bit32.extract
  19. _G.bit32.replace
  20. _G.bit32.bxor
  21. _G.bit32.band
  22. _G.bit32.bnot
  23. _G.bit32.lrotate
  24. _G.bit32.arshift
  25. _G.bit32.rshift
  26. _G.bit32.lshift
  27. _G.bit32.bor
  28. _G._G
  29. _G.coroutine
  30. _G.coroutine.running
  31. _G.coroutine.wrap
  32. _G.coroutine.create
  33. _G.coroutine.resume
  34. _G.coroutine.status
  35. _G.coroutine.yield
  36. _G.assert
  37. _G.type
  38. _G.print
  39. _G.module
  40. _G.debug
  41. _G.debug.gethook
  42. _G.debug.traceback
  43. _G.debug.setuservalue
  44. _G.debug.setupvalue
  45. _G.debug.getlocal
  46. _G.debug.setmetatable
  47. _G.debug.getuservalue
  48. _G.debug.setlocal
  49. _G.debug.getregistry
  50. _G.debug.sethook
  51. _G.debug.getupvalue
  52. _G.debug.upvalueid
  53. _G.debug.debug
  54. _G.debug.upvaluejoin
  55. _G.debug.getmetatable
  56. _G.debug.getinfo
  57. _G.rawequal
  58. _G._VERSION =>Lua5.2
  59. _G.string
  60. _G.string.len
  61. _G.string.find
  62. _G.string.gsub
  63. _G.string.gmatch
  64. _G.string.reverse
  65. _G.string.byte
  66. _G.string.format
  67. _G.string.rep
  68. _G.string.match
  69. _G.string.dump
  70. _G.string.upper
  71. _G.string.sub
  72. _G.string.char
  73. _G.string.lower
  74. _G.pcall
  75. _G.table
  76. _G.table.concat
  77. _G.table.pack
  78. _G.table.insert
  79. _G.table.sort
  80. _G.table.remove
  81. _G.table.maxn
  82. _G.table.unpack
  83. _G.unpack
  84. _G.package
  85. _G.package.seeall
  86. _G.package.config =>/
  87. ;
  88. ?
  89. !
  90. -
  91. _G.package.cpath =>/usr/local/lib/lua/5.2/?.so;/usr/local/lib/lua/5.2/loadall.so;./?.so
  92. _G.package.searchers
  93. _G.package<