Advertisement
revolucas

(Lua) Print Table function

Feb 6th, 2017
1,071
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.78 KB | None | 0 0
  1. --[[
  2. Most pure lua print table functions I've seen have a problem with deep recursion and tend to cause a stack overflow when going too deep. This print table function that I've written does not have this problem. It should also be capable of handling really large tables due to the way it handles concatenation. In my personal usage of this function, it outputted 63k lines to file in about a second.
  3.  
  4. The output also keeps lua syntax and the script can easily be modified for simple persistent storage by writing the output to file if modified to allow only number, boolean, string and table data types to be formatted.
  5.  
  6. author: Alundaio (aka Revolucas)
  7. --]]
  8.  
  9. function print_table(node)
  10.     -- to make output beautiful
  11.     local function tab(amt)
  12.         local str = ""
  13.         for i=1,amt do
  14.             str = str .. "\t"
  15.         end
  16.         return str
  17.     end
  18.  
  19.     local cache, stack, output = {},{},{}
  20.     local depth = 1
  21.     local output_str = "{\n"
  22.  
  23.     while true do
  24.         local size = 0
  25.         for k,v in pairs(node) do
  26.             size = size + 1
  27.         end
  28.  
  29.         local cur_index = 1
  30.         for k,v in pairs(node) do
  31.             if (cache[node] == nil) or (cur_index >= cache[node]) then
  32.                
  33.                 if (string.find(output_str,"}",output_str:len())) then
  34.                     output_str = output_str .. ",\n"
  35.                 elseif not (string.find(output_str,"\n",output_str:len())) then
  36.                     output_str = output_str .. "\n"
  37.                 end
  38.  
  39.                 -- This is necessary for working with HUGE tables otherwise we run out of memory using concat on huge strings
  40.                 table.insert(output,output_str)
  41.                 output_str = ""
  42.                
  43.                 local key
  44.                 if (type(k) == "number" or type(k) == "boolean") then
  45.                     key = "["..tostring(k).."]"
  46.                 else
  47.                     key = "['"..tostring(k).."']"
  48.                 end
  49.  
  50.                 if (type(v) == "number" or type(v) == "boolean") then
  51.                     output_str = output_str .. tab(depth) .. key .. " = "..tostring(v)
  52.                 elseif (type(v) == "table") then
  53.                     output_str = output_str .. tab(depth) .. key .. " = {\n"
  54.                     table.insert(stack,node)
  55.                     table.insert(stack,v)
  56.                     cache[node] = cur_index+1
  57.                     break
  58.                 else
  59.                     output_str = output_str .. tab(depth) .. key .. " = '"..tostring(v).."'"
  60.                 end
  61.  
  62.                 if (cur_index == size) then
  63.                     output_str = output_str .. "\n" .. tab(depth-1) .. "}"
  64.                 else
  65.                     output_str = output_str .. ","
  66.                 end
  67.             else
  68.                 -- close the table
  69.                 if (cur_index == size) then
  70.                     output_str = output_str .. "\n" .. tab(depth-1) .. "}"
  71.                 end
  72.             end
  73.  
  74.             cur_index = cur_index + 1
  75.         end
  76.  
  77.         if (#stack > 0) then
  78.             node = stack[#stack]
  79.             stack[#stack] = nil
  80.             depth = cache[node] == nil and depth + 1 or depth - 1
  81.         else
  82.             break
  83.         end
  84.     end
  85.  
  86.     -- This is necessary for working with HUGE tables otherwise we run out of memory using concat on huge strings
  87.     table.insert(output,output_str)
  88.     output_str = table.concat(output)
  89.    
  90.     print(output_str)
  91. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement