使用lua实现php的print_r()函数功能

发布时间:2022-04-19 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了使用lua实现php的print_r()函数功能脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

之前写了一些类似php的函数,下面再来一个PRint_r()函数,代码如下:

复制代码 代码如下:

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) 

脚本宝典总结

以上是脚本宝典为你收集整理的使用lua实现php的print_r()函数功能全部内容,希望文章能够帮你解决使用lua实现php的print_r()函数功能所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:lua