遍历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. Lua 5.2.1 Copyright (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) do print(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" and not v.package then
  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" and not v.package then
  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 => Lua 5.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.  
  92. _G.package.cpath => /usr/local/lib/lua/5.2/?.so;/usr/local/lib/lua/5.2/loadall.so;./?.so
  93. _G.package.searchers
  94. _G.package.searchers.1
  95. _G.package.searchers.2
  96. _G.package.searchers.3
  97. _G.package.searchers.4
  98. _G.package.path => /usr/local/share/lua/5.2/?.lua;/usr/local/share/lua/5.2/?/init.lua;/usr/local/lib/lua/5.2/?.lua;/usr/local/lib/lua/5.2/?/init.lua;./?.lua
  99. _G.package.searchpath
  100. _G.package.loadlib
  101. _G.package.preload
  102. _G.package.loaded
  103. _G.package.loaders
  104. _G.package.loaders.1
  105. _G.package.loaders.2
  106. _G.package.loaders.3
  107. _G.package.loaders.4
  108. _G.dofile
  109. _G.error
  110. _G.loadstring
  111. _G.io
  112. _G.io.stdin
  113. _G.io.tmpfile
  114. _G.io.stderr
  115. _G.io.stdout
  116. _G.io.input
  117. _G.io.type
  118. _G.io.flush
  119. _G.io.lines
  120. _G.io.write
  121. _G.io.output
  122. _G.io.close
  123. _G.io.open
  124. _G.io.read
  125. _G.io.popen
  126. _G.collectgarbage
  127. _G.xpcall
  128. _G.math
  129. _G.math.atan2
  130. _G.math.pi
  131. _G.math.asin
  132. _G.math.fmod
  133. _G.math.log10
  134. _G.math.cosh
  135. _G.math.acos
  136. _G.math.sqrt
  137. _G.math.log
  138. _G.math.tan
  139. _G.math.deg
  140. _G.math.modf
  141. _G.math.sin
  142. _G.math.pow
  143. _G.math.atan
  144. _G.math.tanh
  145. _G.math.frexp
  146. _G.math.randomseed
  147. _G.math.sinh
  148. _G.math.rad
  149. _G.math.random
  150. _G.math.abs
  151. _G.math.min
  152. _G.math.max
  153. _G.math.ldexp
  154. _G.math.huge
  155. _G.math.floor
  156. _G.math.exp
  157. _G.math.cos
  158. _G.math.ceil
  159. _G.ipairs
  160. _G.setmetatable
  161. _G.rawget
  162. _G.loadfile
  163. _G.re_print
  164. _G.os
  165. _G.os.clock
  166. _G.os.exit
  167. _G.os.date
  168. _G.os.tmpname
  169. _G.os.getenv
  170. _G.os.time
  171. _G.os.rename
  172. _G.os.setlocale
  173. _G.os.execute
  174. _G.os.remove
  175. _G.os.difftime
  176. _G.tonumber
  177. _G.getmetatable
  178. _G.load
  179. _G.require
  180. _G.pairs
  181. _G.tostring
  182. _G.select
  183. _G.rawset
  184. _G.rawlen
  185. >
本文出自:http://guiquanz.me/2012/08/01/lua_global_var/
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值