Advertisement
Lanzr

iota2str

Jun 9th, 2024 (edited)
463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.94 KB | None | 0 0
  1. function table2str(t)
  2.     local function serialize(tbl)
  3.         local tmp = {}
  4.         for k, v in pairs(tbl) do
  5.             local k_type = type(k)
  6.             local v_type = type(v)
  7.             local key = (k_type == "string" and "[\"" .. k .. "\"]=") or (k_type == "number" and "")
  8.             local value = (v_type == "table" and serialize(v)) or (v_type == "boolean" and tostring(v)) or (v_type == "string" and "\"" .. v .. "\"") or (v_type == "number" and v)
  9.             tmp[#tmp + 1] = key and value and tostring(key) .. tostring(value) or nil
  10.         end
  11.         if table.maxn(tbl) == 0 then
  12.             return "\n[\"\"] = {" .. table.concat(tmp, ",") .. "},"
  13.         else
  14.             return "[" .. table.concat(tmp, "") .. "\n]"
  15.         end
  16.     end
  17.     assert(type(t) == "table")
  18.     return serialize(t)
  19. end
  20.  
  21. dev = peripheral.find("focal_port")
  22. d = dev.readIota()
  23. str = table2str(d)
  24. f = io.open("data","w")
  25. f.write(f,str)
  26. f.close(f)
  27.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement