Módulo:Print: mudanças entre as edições
Aspeto
Criou página com ' function p.table_to_string(tbl) local result = "{" for k, v in pairs(tbl) do -- Converte a chave para string local key = type(k) == "string" and '"'..k..'"' or tostring(k) -- Converte o valor para string local value if type(v) == "table" then value = table_to_string(v) -- Recursão para tabelas aninhadas elseif type(v) == "string" then value = '"'..v..'"' else value = tostring(v) end result = result.."...' |
Sem resumo de edição |
||
Linha 1: | Linha 1: | ||
function | function table_to_string(tbl) | ||
local result = "{" | local result = "{" | ||
for k, v in pairs(tbl) do | for k, v in pairs(tbl) do |
Edição das 21h58min de 29 de março de 2025
A documentação para este módulo pode ser criada em Módulo:Print/doc
function table_to_string(tbl)
local result = "{"
for k, v in pairs(tbl) do
-- Converte a chave para string
local key = type(k) == "string" and '"'..k..'"' or tostring(k)
-- Converte o valor para string
local value
if type(v) == "table" then
value = table_to_string(v) -- Recursão para tabelas aninhadas
elseif type(v) == "string" then
value = '"'..v..'"'
else
value = tostring(v)
end
result = result.."["..key.."] = "..value..", "
end
-- Remove a vírgula e espaço extras no final, se existirem
if result:sub(-2) == ", " then
result = result:sub(1, -3)
end
return result.."}"
end