Print Table Lua

[Solved] Print Table Lua | Lua - Code Explorer | yomemimo.com
Question : print table lua

Answered by : blue-badger-8urgw6brwzjc

function print_table(node) local cache, stack, output = {},{},{} local depth = 1 local output_str = "{\n" while true do local size = 0 for k,v in pairs(node) do size = size + 1 end local cur_index = 1 for k,v in pairs(node) do if (cache[node] == nil) or (cur_index >= cache[node]) then if (string.find(output_str,"}",output_str:len())) then output_str = output_str .. ",\n" elseif not (string.find(output_str,"\n",output_str:len())) then output_str = output_str .. "\n" end -- This is necessary for working with HUGE tables otherwise we run out of memory using concat on huge strings table.insert(output,output_str) output_str = "" local key if (type(k) == "number" or type(k) == "boolean") then key = "["..tostring(k).."]" else key = "['"..tostring(k).."']" end if (type(v) == "number" or type(v) == "boolean") then output_str = output_str .. string.rep('\t',depth) .. key .. " = "..tostring(v) elseif (type(v) == "table") then output_str = output_str .. string.rep('\t',depth) .. key .. " = {\n" table.insert(stack,node) table.insert(stack,v) cache[node] = cur_index+1 break else output_str = output_str .. string.rep('\t',depth) .. key .. " = '"..tostring(v).."'" end if (cur_index == size) then output_str = output_str .. "\n" .. string.rep('\t',depth-1) .. "}" else output_str = output_str .. "," end else -- close the table if (cur_index == size) then output_str = output_str .. "\n" .. string.rep('\t',depth-1) .. "}" end end cur_index = cur_index + 1 end if (size == 0) then output_str = output_str .. "\n" .. string.rep('\t',depth-1) .. "}" end if (#stack > 0) then node = stack[#stack] stack[#stack] = nil depth = cache[node] == nil and depth + 1 or depth - 1 else break end end -- This is necessary for working with HUGE tables otherwise we run out of memory using concat on huge strings table.insert(output,output_str) output_str = table.concat(output) print(output_str)
end

Source : | Last Update : Sun, 27 Dec 20

Question : lua print contents of table

Answered by : orion

for k,v in pairs(table) do print(v)
end

Source : | Last Update : Thu, 26 Mar 20

Question : lua print all elements table

Answered by : cooks-tree-boa

local people = { { name = "Fred", address = "16 Long Street", phone = "123456" }, { name = "Wilma", address = "16 Long Street", phone = "123456" }, { name = "Barney", address = "17 Long Street", phone = "123457" }
}
for index, data in ipairs(people) do print(index) for key, value in pairs(data) do print('\t', key, value) end
end

Source : https://stackoverflow.com/questions/41942289/display-contents-of-tables-in-lua | Last Update : Sat, 23 May 20

Question : print a table in lua

Answered by : upset-unicorn-0h9vw1lfqbwi

function dump(o) if type(o) == 'table' then local s = '{ ' for k,v in pairs(o) do if type(k) ~= 'number' then k = '"'..k..'"' end s = s .. '['..k..'] = ' .. dump(v) .. ',' end return s .. '} ' else return tostring(o) end
end

Source : https://stackoverflow.com/questions/9168058/how-to-dump-a-table-to-console | Last Update : Fri, 06 May 22

Question : lua print table

Answered by : ugliest-unicorn-1ba63bt64u35

function print_table(node) local cache, stack, output = {},{},{} local depth = 1 local output_str = "{\n" while true do local size = 0 for k,v in pairs(node) do size = size + 1 end local cur_index = 1 for k,v in pairs(node) do if (cache[node] == nil) or (cur_index >= cache[node]) then if (string.find(output_str,"}",output_str:len())) then output_str = output_str .. ",\n" elseif not (string.find(output_str,"\n",output_str:len())) then output_str = output_str .. "\n" end -- This is necessary for working with HUGE tables otherwise we run out of memory using concat on huge strings table.insert(output,output_str) output_str = "" local key if (type(k) == "number" or type(k) == "boolean") then key = "["..tostring(k).."]" else key = "['"..tostring(k).."']" end if (type(v) == "number" or type(v) == "boolean") then output_str = output_str .. string.rep('\t',depth) .. key .. " = "..tostring(v) elseif (type(v) == "table") then output_str = output_str .. string.rep('\t',depth) .. key .. " = {\n" table.insert(stack,node) table.insert(stack,v) cache[node] = cur_index+1 break else output_str = output_str .. string.rep('\t',depth) .. key .. " = '"..tostring(v).."'" end if (cur_index == size) then output_str = output_str .. "\n" .. string.rep('\t',depth-1) .. "}" else output_str = output_str .. "," end else -- close the table if (cur_index == size) then output_str = output_str .. "\n" .. string.rep('\t',depth-1) .. "}" end end cur_index = cur_index + 1 end if (size == 0) then output_str = output_str .. "\n" .. string.rep('\t',depth-1) .. "}" end if (#stack > 0) then node = stack[#stack] stack[#stack] = nil depth = cache[node] == nil and depth + 1 or depth - 1 else break end end -- This is necessary for working with HUGE tables otherwise we run out of memory using concat on huge strings table.insert(output,output_str) output_str = table.concat(output) print(output_str)
end

Source : https://stackoverflow.com/questions/9168058/how-to-dump-a-table-to-console | Last Update : Sat, 11 Jun 22

Question : lua print table as string

Answered by : ege-bilecen

-- Usage: GetTableString(table) | "depth" is recursive param, ignore it.
-- This function will print the table in formatted way.
function GetTableString(o, depth) if type(o) ~= "table" then return "Given parameter is not a table." end local pad = function(str, msg, depth) local padding = string.rep(str, depth) return padding..msg end depth = depth or 0 local tabLen = (depth + 1) * 4 local displayStr = "" displayStr = displayStr..pad(" ", "{", 0).."\n" for k,v in pairs(o) do displayStr = displayStr..pad(" ", k.." = ", tabLen) if type(v) == "table" then displayStr = displayStr..DebugUtil.getTableString(v, depth+1) else displayStr = displayStr..tostring(v).." ("..type(v)..")" end displayStr = displayStr.."\n" end displayStr = displayStr..pad(" ", "}", depth * 4).."\n" return displayStr
end

Source : | Last Update : Fri, 02 Sep 22

Answers related to print table lua

Code Explorer Popular Question For Lua