使用lua實現(xiàn)php的print_r()函數(shù)功能
更新時間:2014年11月03日 16:04:34 投稿:hebedich
筆者比較熟悉php,所以一直在做一些使用lua來實現(xiàn)php中函數(shù)的功能,算是自己對lua理解的一個小測試吧
之前寫了一些類似php的函數(shù),下面再來一個print_r()函數(shù),代碼如下:
復(fù)制代碼 代碼如下:
function pr (t, name, indent)
local tableList = {}
function table_r (t, name, indent, full)
local id = not full and name or type(name)~="number" and tostring(name) or '['..name..']'
local tag = indent .. id .. ' = '
local out = {} -- result
if type(t) == "table" then
if tableList[t] ~= nil then
table.insert(out, tag .. '{} -- ' .. tableList[t] .. ' (self reference)')
else
tableList[t]= full and (full .. '.' .. id) or id
if next(t) then -- Table not empty
table.insert(out, tag .. '{')
for key,value in pairs(t) do
table.insert(out,table_r(value,key,indent .. '| ',tableList[t]))
end
table.insert(out,indent .. '}')
else table.insert(out,tag .. '{}') end
end
else
local val = type(t)~="number" and type(t)~="boolean" and '"'..tostring(t)..'"' or tostring(t)
table.insert(out, tag .. val)
end
return table.concat(out, '\n')
end
return table_r(t,name or 'Value',indent or '')
end
function print_r (t, name)
print(pr(t,name))
end
local a = {x=1, y=2, label={text='hans', color='blue'}, list={'a','b','c'}}
print_r(a)
您可能感興趣的文章:
- php中理解print EOT分界符和echo EOT的用法區(qū)別小結(jié)
- PHP學(xué)習(xí)之輸出字符串(echo,print,printf,print_r和var_dump)
- 用js寫了一個類似php的print_r輸出換行功能
- php輸出echo、print、print_r、printf、sprintf、var_dump的區(qū)別比較
- PHP echo,print,printf,sprintf函數(shù)之間的區(qū)別與用法詳解
- 用nodejs實現(xiàn)PHP的print_r函數(shù)代碼
- PHP中echo,print_r與var_dump區(qū)別分析
- php中print(),print_r(),echo()的區(qū)別詳解
相關(guān)文章
Lua教程(五):C/C++操作Lua數(shù)組和字符串示例
這篇文章主要介紹了Lua教程(五):C/C++操作Lua數(shù)組和字符串示例,本文同時還講解了如何在C/C++函數(shù)里面存儲Lua狀態(tài),需要的朋友可以參考下2014-09-09Lua中的協(xié)同程序之resume-yield間的數(shù)據(jù)返回研究
這篇文章主要介紹了Lua中的協(xié)同程序之resume-yield間的數(shù)據(jù)返回研究本文講解了resume的參數(shù)、resume函數(shù)的第二個返回值、yield的返回值等內(nèi)容,需要的朋友可以參考下2014-09-09