• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

tableString(table) - Table to String

Mazen

Developer
Joined
Aug 20, 2007
Messages
612
Reaction score
38
Location
Sweden
Hello OTLand. Since I know by experience that sometimes it's hard to understand what's inside tables that you didn't write, that most of the times are returned by functions.

A good way to see exactly how a table looks like is to make it as a string.

Here is the script the will convert any table to a string:
Lua:
function tableString(tb, showInt)
	local tb_string = ""
	local function table_calculate(tb, sub, key)
		if type(tb) == "table" then
			tb_string = tb_string .. ((type(key) == "string") and (key .. " = ") or ((type(key) == "number" and showInt) and "[".. key .."] = " or "")) .. "{"
			for k, v in pairs(tb) do
				if type(v) == "table" then
					table_calculate(v, (next(tb, k) and true or false), k)
				else
					if type(v) == "string" then
						v = "\"" .. v .. "\""
					elseif type(v) == "boolean" then
						v = (v and "true" or "false")
					elseif type(v) == "nil" then
						v = "nil"
					elseif type(v) == "function" then
						v = "function" .. "()"
					end
					tb_string = tb_string .. (type(k) == "string" and (k .. " = ") or (showInt and "[".. k .."] = " or "")) .. v .. (next(tb, k) and ", " or "")
				end
			end
			tb_string = tb_string .. "}" .. (sub and ", " or "")
		else
			return false
		end
		return true
	end
	
	-- Write maintable.
	if table_calculate(tb, false) then
		return tb_string
	else
		return false
	end
end
This one can be useful for you LUA scripters out there. The function is tested and will give you a perfect result.

Try it out yourself. ;)

Note: It still doesn't support threads yet.
 
Last edited:
i have the same function in OTAL.
tableToString
saveTable
loadTable
Lua:
function tableToString(tb) -- by Mock
	if type(tb) ~= "table" then
		return nil, error("bad argument #1 to 'saveTable' (table expected, got "..type(tb)..")", 2)
	end
	local str = "{"
	for k,d in pairs(tb) do
		if type(k) == 'string' then
			if type(d) == 'string' then
				str = str..""..k.."='"..d.."',"
			elseif type(d) == 'number' or type(d) == 'boolean' then
				str = str..""..k.."="..tostring(d)..","
			elseif type(d) == 'table' then
				str = str..'{'
				for e,f in pairs(d) do
					if type(e) == 'string' then
						if type(f) == 'string' then
							str = str..""..e.."='"..f.."',"
						elseif type(f) == 'number' or type(e) == 'boolean' then
							str = str..""..e.."="..tostring(f)..","
						elseif type(f) == 'table' then
							str = str..""..e.."="..tableToString(f)..","
						end
					elseif type(e) == 'number' then
						if type(f) == 'string' then
							str = str.."["..e.."]='"..f.."',"
						elseif type(f) == 'number' or type(f) == 'boolean' then
							str = str.."["..e.."]="..tostring(f)..","
						elseif type(f) == 'table' then
							str = str.."["..e.."]="..tableToString(f)..","
						end
					end
				end
				str = str..'},'
			end
		elseif type(k) == 'number' then
			if type(d) == 'string' then
				str = str.."["..k.."]='"..d.."',"
			elseif type(d) == 'number' or type(d) == 'boolean' then
				str = str.."["..k.."]="..tostring(d)..","
			elseif type(d) == 'table' then
				str = str..'{'
				for e,f in pairs(d) do
					if type(e) == 'string' then
						if type(f) == 'string' then
							str = str..""..e.."='"..f.."',"
						elseif type(f) == 'number' or type(e) == 'boolean' then
							str = str..""..e.."="..tostring(f)..","
						elseif type(f) == 'table' then
							str = str..""..e.."="..tableToString(f)..","
						end
					elseif type(e) == 'number' then
						if type(f) == 'string' then
							str = str.."["..e.."]='"..f.."',"
						elseif type(f) == 'number' or type(f) == 'boolean' then
							str = str.."["..e.."]="..tostring(f)..","
						elseif type(f) == 'table' then
							str = str.."["..e.."]="..tableToString(f)..","
						end
					end
				end
				str = str..'},'
			end
		end
	end
	local str = str.."}"
	if string.sub(str,string.len(str)-2,string.len(str)-2) == "," then
		str = string.sub(str,0,string.len(str)-3).."}"
	end
	return str
end
 
@up, didn't know about that. xD

Update: Fixed a few bugs and made an option to show numeric key values. Example:
Lua:
my_table = {"hi", cake = "blalalala"}
print(tableString(my_table, true))

Will return:
Lua:
{[1] = "hi", cake = "blalalala"}

Use tableString(my_table, true) to show numeric key values, otherwise just use tableString(my_table).
 
good ^^
my version save to string all kind of table but it dont save functions and userdatas.
Lua:
local tabl = {
{},[584]={},true,false,['omg'] = 'fail',t=q,1,5,7,{{{{{{{true,false,'var'}}}}}}}
}
 
Back
Top