HenryEx

Print table to LUA console

Aug 18th, 2026
4,577
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.70 KB | Source Code | 0 0
  1. -- from: https://stackoverflow.com/questions/9168058/how-to-dump-a-table-to-console
  2. -- author: Alundaio
  3. -- Prints a table
  4. -- Modded for custom indents, default indent is 2 spaces, can be customized (minimum 1)
  5. function print_table(node, indent)
  6.   if not indent then indent = 2 end
  7.   if indent < 1 then indent = 1 end
  8.   local cache, stack, output = {},{},{}
  9.   local depth = 1
  10.   local output_str = "{\n"
  11.  
  12.   while true do
  13.     local size = 0
  14.     for k,v in pairs(node) do
  15.       size = size + 1
  16.     end
  17.  
  18.     local cur_index = 1
  19.     for k,v in pairs(node) do
  20.       if (cache[node] == nil) or (cur_index >= cache[node]) then
  21.  
  22.         if (string.find(output_str,"}",output_str:len())) then
  23.           output_str = output_str .. ",\n"
  24.         elseif not (string.find(output_str,"\n",output_str:len())) then
  25.           output_str = output_str .. "\n"
  26.         end
  27.  
  28.         -- This is necessary for working with HUGE tables otherwise we run out of memory using concat on huge strings
  29.         table.insert(output,output_str)
  30.         output_str = ""
  31.  
  32.         local key
  33.         if (type(k) == "number" or type(k) == "boolean") then
  34.           key = "["..tostring(k).."]"
  35.         else
  36.           key = "['"..tostring(k).."']"
  37.         end
  38.  
  39.         if (type(v) == "number" or type(v) == "boolean") then
  40.           output_str = output_str .. string.rep(' ',depth * indent) .. key .. " = "..tostring(v)
  41.         elseif (type(v) == "table") then
  42.           output_str = output_str .. string.rep(' ',depth * indent) .. key .. " = {\n"
  43.           table.insert(stack,node)
  44.           table.insert(stack,v)
  45.           cache[node] = cur_index+1
  46.           break
  47.         else
  48.           output_str = output_str .. string.rep(' ',depth * indent) .. key .. " = '"..tostring(v).."'"
  49.         end
  50.  
  51.         if (cur_index == size) then
  52.           output_str = output_str .. "\n" .. string.rep(' ',(depth-1) * indent) .. "}"
  53.         else
  54.           output_str = output_str .. ","
  55.         end
  56.       else
  57.         -- close the table
  58.         if (cur_index == size) then
  59.           output_str = output_str .. "\n" .. string.rep(' ',(depth-1) * indent) .. "}"
  60.         end
  61.       end
  62.  
  63.       cur_index = cur_index + 1
  64.     end
  65.  
  66.     if (size == 0) then
  67.       output_str = output_str .. "\n" .. string.rep(' ',(depth-1) * indent) .. "}"
  68.     end
  69.  
  70.     if (#stack > 0) then
  71.       node = stack[#stack]
  72.       stack[#stack] = nil
  73.       depth = cache[node] == nil and depth + 1 or depth - 1
  74.     else
  75.       break
  76.     end
  77.   end
  78.  
  79.   -- This is necessary for working with HUGE tables otherwise we run out of memory using concat on huge strings
  80.   table.insert(output,output_str)
  81.   output_str = table.concat(output)
  82.  
  83.   print(output_str)
  84.   return output_str
  85. end
Advertisement
Add Comment
Please, Sign In to add comment