Lua table output function (nested tables can be output)

Source: Internet
Author: User
Function printtable (O, F, B)
If type (f )~ = "Function" and F ~ = Nil then
Error ("expected second argument % s is a function", tostring (f ))
End
If type (B )~ = "Boolean" and B ~ = Nil then
Error ("expected third argument % s is a Boolean", tostring (B ))
End
P = f or IO. Write
B = B or false
If type (o) = "Number" or
Type (o) = "function" or
Type (o) = "Boolean" or
Type (o) = "nil" then
P (tostring (o ))
Elseif type (o) = "string" then
P (string. Format ("% Q", O ))
Elseif type (o) = "table" then
P ("{/N ")
For K, V in pairs (o) Do
If B then
P ("[")
End

Printtable (K, P, B)

If B then
P ("]")
End

P ("= ")
Printtable (V, p, B)
P (",/N ")
End
P ("}")

End
End

Recently, I learned Lua because of my work needs. It was very interesting, and even gave me the opportunity to go back and continue to learn the python I have learned before.
Because we often use Vim to write Lua, debugging is not very convenient, So we wrote the above function according to programming in Lua, and it is quite convenient to use.
Specify the second parameter when output to the file, or change the IO. Write behavior through Io. output. The third parameter specifies the output
When you get to the file and can read the [] number again, let's not talk about the specific reason. Let's just look at programming in Lua.

The following is an example:
A = {[{100,200}] = {300,400}, 200, {300,500}, abc = "ABC "}
Printtable (A, Io. Write, true)

The output result is as follows:
{
[1] = 200,
[2] = {
[1] = 300,
[2] = 500,
},
[{
[1] = 100,
[2] = 200,
}] = {
[1] = 300,
[2] = 400,
},
["ABC"] = "ABC ",
}

In fact, it is still a legal Lua statement, which can be used as a serialization statement or configuration file. It has not been strictly tested and can only be used as a reference.
For beginners, please note that you can add module ("printtable", package. seeall) before this function and save the file in
Similar to the lualibs library directory, you can use this function by using require "printtable" in your own program.

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.