Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

32
LINES

< > BotCompany Repo | #151 // luaPrintVars

Lua code

1  
local serpent = grab('#158')
2  
local function tableToLua(t)
3  
  return "return "..serpent.block(t)
4  
end
5  
6  
local function isSimpleTable(tbl, seen)
7  
  if seen == nil then seen = {} end
8  
  local seen = {}
9  
  if seen[tbl] then return false end -- has cycles, not simple
10  
  local t = type(tbl)
11  
  if t == 'number' or t == 'string' then return true end
12  
  if t ~= 'table' then return false end
13  
  seen[tbl] = true
14  
  for k, v in pairs(tbl) do
15  
    if not isSimpleTable(k, seen)
16  
      or not isSimpleTable(t, seen) then
17  
        return false
18  
    end
19  
  end
20  
end
21  
22  
local lines = {}
23  
for k, v in pairs(_G) do
24  
  if type(v) == 'number' then
25  
    table.insert(lines, k..'='..tostring(v))
26  
  elseif type(v) == 'string' then
27  
    table.insert(lines, k..'='..('%q'):format(v))
28  
  elseif type(v) == 'table' and isSimpleTable(v) then
29  
    table.insert(lines, k..'='..tableToLua(v))
30  
  end
31  
end
32  
return table.concat(lines, "\n")

Author comment

Special snippet. Used for updating objects.

test run  test run with input  download  show line numbers   

Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Image recognition results

Recognizer Recognition Result Visualize Recalc
#308 913 [visualize]

Snippet ID: #151
Snippet name: luaPrintVars
Eternal ID of this version: #151/1
Text MD5: 71efc65a03bb83a6114d8a1c4eb32277
Author: stefan
Category: internal helpers
Type: Lua code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2014-01-13 05:18:29
Source code size: 913 bytes / 32 lines
Pitched / IR pitched: Yes / Yes
Views / Downloads: 839 / 213
Referenced in: [show references]