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.