2017-02-22 34 views
1

Я хочу сбросить таблицу _G. в таблице _G есть другая таблица и дамп (встроенная таблица). и я хочу иметь хороший формат. У меня есть пример, но использовать его дамп _G таблица есть некоторые проблемыКак сбросить все содержимое таблицы _G

function print_table(node) 
-- to make output beautiful 
local function tab(amt) 
    local str = "" 
    for i=1,amt do 
     str = str .. "\t" 
    end 
    return str 
end 

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 = "[" ..type(k).. ":"..tostring(k).."]" 
      else 
       key = "['"..type(k).. ":"..tostring(k).."']" 
      end 

      if (type(v) == "number" or type(v) == "boolean") then 
       output_str = output_str .. tab(depth) .. key .. " = "..tostring(v) 
      elseif (type(v) == "table") then 
       output_str = output_str .. tab(depth) .. key .. " = {\n" 
       table.insert(stack,node) 
       table.insert(stack,v) 
       cache[node] = cur_index+1 
       break 
      else 
       output_str = output_str .. tab(depth) .. key .. " = '"..tostring(v).."'" 
      end 

      if (cur_index == size) then 
       output_str = output_str .. "\n" .. tab(depth-1) .. "}" 
      else 
       output_str = output_str .. "," 
      end 
     else 
      -- close the table 
      if (cur_index == size) then 
       output_str = output_str .. "\n" .. tab(depth-1) .. "}" 
      end 
     end 

     cur_index = cur_index + 1 
    end 

    if (size == 0) then 
     output_str = output_str .. "\n" .. tab(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 

это приведет журнал

------start------- 
['function:add_msg_define'] = 'function: 0x7c6baa98', 
['number:BLEND_DST_RGB'] = 32968, 
['function:_attachShader'] = 'function: 0x7c626cf0', 
['number:INVALID_OPERATION'] = 1282, 
['function:drawArrays'] = 'function: 0x7c627480', 
['number:SRGB8_ALPHA8_EXT'] = 35907, 
['number:CCW'] = 2 
------end------- 

я не знаю почему, как исправить эту функцию

+0

См. Также [Lua live demo] (https://www.lua.org/cgi-bin/demo?globals). – lhf

ответ

0

Я использую это:

-- deep dumps the contents of the table and it's contents' contents 
function deepdump(tbl) 
    local checklist = {} 
    local function innerdump(tbl, indent) 
     checklist[ tostring(tbl) ] = true 
     for k,v in pairs(tbl) do 
      print(indent..k,v,type(v),checklist[ tostring(tbl) ]) 
      if (type(v) == "table" and not checklist[ tostring(v) ]) then innerdump(v,indent.." ") end 
     end 
    end 
    print("=== DEEPDUMP -----") 
    checklist[ tostring(tbl) ] = true 
    innerdump(tbl, "") 
    print("------------------") 
end 

https://gist.github.com/HoraceBury/9321964

+0

спасибо, этот код полезен, но я хочу, чтобы пользователь мой код, потому что структура журнала очень понятна. Отношения в таблице очень четкие, вы можете попробовать. – Zero