zoukankan      html  css  js  c++  java
  • lua中打印所以类型功能实现table嵌套table

    lua中打印所以类型功能实现

    本人測试 number、string、bool、nil、table嵌套table、userdata没问题 共享一下有什么问题请拍砖 代码例如以下 

    cclog = function( ... )
        local tv = "
    "
        local xn = 0
        local function tvlinet(xn)
            -- body
            for i=1,xn do
                tv = tv.."	"
            end
        end
    
        local function printTab(i,v)
            -- body
            if type(v) == "table" then
                tvlinet(xn)
                xn = xn + 1
                tv = tv..""..i..":Table{
    "
                table.foreach(v,printTab)
                tvlinet(xn)
                tv = tv.."}
    "
                xn = xn - 1
            elseif type(v) == nil then
                tvlinet(xn)
                tv = tv..i..":nil
    "
            else
                tvlinet(xn)
                tv = tv..i..":"..tostring(v).."
    " 
            end
        end
        local function dumpParam(tab)
            for i=1, #tab do  
                if tab[i] == nil then 
                    tv = tv.."nil	"
                elseif type(tab[i]) == "table" then 
                    xn = xn + 1
                    tv = tv.."
    table{
    "
                    table.foreach(tab[i],printTab)
                    tv = tv.."	}
    "
                else
                    tv = tv..tostring(tab[i]).."	"
                end
            end
        end
        local x = ...
        if type(x) == "table" then
            table.foreach(x,printTab)
        else
            dumpParam({...})
            -- table.foreach({...},printTab)
        end
        print(tv)
    end

    使用示比例如以下代码

        local ttt = {23,aa=23,23,
                        {bbb=23,"dsd",false,nil,
                            {32,ccc="23dd",
                                {23,"sdfsdf",
                                    {234,addd="23233jjjjsdOK"}
                                }
                            }
                        },
                        {dd = "sd",23},
                    true
                    }
    
        cclog(23,"sdf",ttt,"sdssssf",323223,false)
    输出日志例如以下:
    Cocos2d: [LUA-print] 
    23	sdf	
    table{
    	1:23
    	2:23
    	3:Table{
    		1:dsd
    		2:false
    		4:Table{
    			1:32
    			2:Table{
    				1:23
    				2:sdfsdf
    				3:Table{
    					1:234
    					addd:23233jjjjsdOK
    					}
    				}
    			ccc:23dd
    			}
    		bbb:23
    		}
    	4:Table{
    		1:23
    		dd:sd
    		}
    	5:true
    	aa:23
    	}
    sdssssf	323223	false	

  • 相关阅读:
    轻量级通用上采样算子-CARAFE
    图像分割-Mask Scoring R-CNN
    对C#Chart控件使用整理
    C#中的三种timer
    C#的三大难点
    将Excel的数据导入DataGridView中(转)
    状态者设计模式
    C# 中 DataTable 使用详解。
    Excel连接字符串在.NET中的应用
    状态机设计思想
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/3885589.html
Copyright © 2011-2022 走看看