现在的位置: 首页Lua>正文
lua实现php的var_dump()函数功能 [原创]
2012年01月20日 Lua lua实现php的var_dump()函数功能 [原创]已关闭评论 ⁄ 被围观 14,244 次+

习惯了php中的var_dump()函数,而如今写lua的时候总习惯使用var_dump()函数,于是就自己动手写了一个类似功能的var_dump()函数。

  1. function var_dump(data, max_level, prefix)   
  2.     if type(prefix) ~= "string" then   
  3.         prefix = ""  
  4.     end   
  5.     if type(data) ~= "table" then   
  6.         print(prefix .. tostring(data))   
  7.     else  
  8.         print(data)   
  9.         if max_level ~= 0 then   
  10.             local prefix_next = prefix .. "    "  
  11.             print(prefix .. "{")   
  12.             for k,v in pairs(data) do  
  13.                 io.stdout:write(prefix_next .. k .. " = ")   
  14.                 if type(v) ~= "table" or (type(max_level) == "number" and max_level <= 1) then   
  15.                     print(v)   
  16.                 else  
  17.                     if max_level == nil then   
  18.                         var_dump(v, nil, prefix_next)   
  19.                     else  
  20.                         var_dump(v, max_level - 1, prefix_next)   
  21.                     end   
  22.                 end   
  23.             end   
  24.             print(prefix .. "}")   
  25.         end   
  26.     end   
  27. end  

本文地址:http://www.92csz.com/05/1034.html
如非注明则为本站原创文章,欢迎转载。转载请注明转载自:moon's blog
 

抱歉!评论已关闭.