码迷,mamicode.com
首页 > 其他好文 > 详细

lua 打印一个table的实现

时间:2018-05-04 01:42:38      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:分享   family   port   owb   bubuko   style   for   image   http   

print("-------------Test-----------------")

local tb = {}

function printProperties(t, csp)
    local parentOffset = csp or ""
    local propertyOffset = parentOffset .. "\t"
    local str = ""
    str = str .. "{" .. "\n"
    for k, v in pairs(t) do
         if type(v)=="table" then
             str = str .. propertyOffset.. k .. " = " .. printProperties(v, propertyOffset) .. ",\n"
         else
             str = str .. propertyOffset.. k .. " = ‘" .. v .. "‘,\n"
         end
    end
    str = string.sub(str, 1, string.len(str) - string.len(",\n")) .. "\n"
    str = str .. parentOffset .. "}"
    return str
end

function printTb( tb )  
  print(printProperties(tb))
end

local myTable = {
    firstName = "Fred",
    lastName = "Bob",
    phoneNumber = "(555) 555-1212",
    age = 30,
    favoriteSports = { "Baseball", "Hockey", "Soccer"},
    favoriteSports = { "Baseball", "Hockey", "Soccer" , ttt = {"T1","T2"}},
    favoriteTeams  = { "Cowboys", "Panthers", "Reds" }
}

printTb(myTable)

  效果:

技术分享图片

 

lua 打印一个table的实现

标签:分享   family   port   owb   bubuko   style   for   image   http   

原文地址:https://www.cnblogs.com/NiYun/p/8988336.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!