Lua obtains the field name in the table.

Source: Internet
Author: User

Suppose there is the following code:

 
 
  1. local t =
  2. {
  3. a = 1,
  4. b = { x = 1, y = 2}
  5. }

I will pass you a table and want to know which fields are in this table, but cannot be obtained directly. In this case, you can use the following method:

 
 
  1. for k, v in pairs (t) do
  2. print(tostring(k), v)
  3. end

We can see that it is okay to convert K into a field string using the tostring function.

However, we can also see that when a table is nested in a table, the nested table cannot be printed out. In this case, is there no way to do this?
Of course not. Someone has already implemented this common requirement. Here we will first provide the print_r version of Yunfeng.

 
 
  1. Print a table in a tree shape,
  2. @ Param: Root Node of the root table
  3. function Utils.print_r(root)
  4. local cache = { [root] = "." }
  5. local function _dump(t,space,name)
  6. local temp = {}
  7. for k,v in pairs(t) do
  8. local key = tostring(k)
  9. if cache[v] then
  10. table.insert(temp,"+" .. key .. " {" .. cache[v].."}")
  11. elseif type(v) == "table" then
  12. local new_key = name .. "." .. key
  13. cache[v] = new_key
  14. table.insert(temp,"+" .. key .. _dump(v,space .. (next(t,k) and "|" or " " ).. string.rep(" ",#key),new_key))
  15. else
  16. table.insert(temp,"+" .. key .. " [" .. tostring(v).."]")
  17. end
  18. end
  19. return table.concat(temp,"\n"..space)
  20. end
  21. print(_dump(root, "" , ""))
  22. end


As you can see, the X and Y fields in the nested table B have been printed, but this version is tree-like. If it is difficult to print complex table structures, at this time, you can take a look at the version

 
 
  1. function Utils.print_lua_table (lua_table, indent)
  2. indent = indent or 0
  3. for k, v in pairs(lua_table) do
  4. if type(k) == "string" then
  5. k = string.format("%q", k)
  6. end
  7. local szSuffix = ""
  8. if type(v) == "table" then
  9. szSuffix = "{"
  10. end
  11. local szPrefix = string.rep(" ", indent)
  12. formatting = szPrefix.."["..k.."]".." = "..szSuffix
  13. if type(v) == "table" then
  14. print(formatting)
  15. print_lua_table(v, indent + 1)
  16. print(szPrefix.."},")
  17. else
  18. local szValue = ""
  19. if type(v) == "string" then
  20. szValue = string.format("%q", v)
  21. else
  22. szValue = tostring(v)
  23. end
  24. print(formatting..szValue..",")
  25. end
  26. end
  27. end

Original: https://gist.github.com/rangercyh/5814003

However, this does not support key-value, so the function is quite tasteless. Please be careful when using it!



From Weizhi note (wiz)

Lua obtains the field name in the table.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.