-
-
Save marcotrosi/163b9e890e012c6a460a to your computer and use it in GitHub Desktop.
--[[ | |
A simple function to print tables or to write tables into files. | |
Great for debugging but also for data storage. | |
When writing into files the 'return' keyword will be added automatically, | |
so the tables can be loaded with 'dofile()' into a variable. | |
The basic datatypes table, string, number, boolean and nil are supported. | |
The tables can be nested and have number and string indices. | |
This function has no protection when writing files without proper permissions and | |
when datatypes other then the supported ones are used. | |
--]] | |
-- t = table | |
-- f = filename [optional] | |
function printTable(t, f) | |
local function printTableHelper(obj, cnt) | |
local cnt = cnt or 0 | |
if type(obj) == "table" then | |
io.write("\n", string.rep("\t", cnt), "{\n") | |
cnt = cnt + 1 | |
for k,v in pairs(obj) do | |
if type(k) == "string" then | |
io.write(string.rep("\t",cnt), '["'..k..'"]', ' = ') | |
end | |
if type(k) == "number" then | |
io.write(string.rep("\t",cnt), "["..k.."]", " = ") | |
end | |
printTableHelper(v, cnt) | |
io.write(",\n") | |
end | |
cnt = cnt-1 | |
io.write(string.rep("\t", cnt), "}") | |
elseif type(obj) == "string" then | |
io.write(string.format("%q", obj)) | |
else | |
io.write(tostring(obj)) | |
end | |
end | |
if f == nil then | |
printTableHelper(t) | |
else | |
io.output(f) | |
io.write("return") | |
printTableHelper(t) | |
io.output(io.stdout) | |
end | |
end |
@csm4502 I finally had the time to try with Lua 5.1.5 and I had no issues. Also tried with Lua 5.3 and it works as well.
If you find the issue let me know what it was. Thank you.
Hi @marcotrosi. I need your help.
I apologise if I am doing something really wrong but I have just started using lua files. I am trying to use a code to transfer a Lua table into another file.
When I run "printTable.lua using Lua53, I am getting the following as seen in the photo:
Do I need to copy the contents of the file with the table to yours? (it has only a table stored in it)
Am I using your file correctly just running straight with Lua 5.3.5?
Hi @marcotrosi. I need your help.
I apologise if I am doing something really wrong but I have just started using lua files. I am trying to use a code to transfer a Lua table into another file.When I run "printTable.lua using Lua53, I am getting the following as seen in the photo:
Do I need to copy the contents of the file with the table to yours? (it has only a table stored in it)
Am I using your file correctly just running straight with Lua 5.3.5?
Hi @Believe82,
you can copy my printTable() function for example to your Lua script and use it there.
for example assume you have file named myLuaScript.lua
<copy paste function here>
-- create a table
YourTable = {1,2,3}
-- print the table using my function
printTable(YourTable)
and then run the script in the shell with ...
lua myLuaScript.lua
if you need more help join me on Telegram
Hi @csm4502. Thanks for the feedback.
Can you tell me which Lua version you used for that?
Thank you