Advertisement
massacring

MassaMainLib

May 5th, 2024 (edited)
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.46 KB | None | 0 0
  1. -- https://stackoverflow.com/a/41943392/22091473
  2. local function tprint(tbl, indent)
  3.     if not indent then indent = 0 end
  4.     local toprint = string.rep(" ", indent) .. "{\r\n"
  5.     indent = indent + 2
  6.     for k, v in pairs(tbl) do
  7.         toprint = toprint .. string.rep(" ", indent)
  8.         if (type(k) == "number") then
  9.             toprint = toprint .. "[" .. k .. "] = "
  10.         elseif (type(k) == "string") then
  11.             toprint = toprint  .. k ..  "= "  
  12.         end
  13.         if (type(v) == "number") then
  14.             toprint = toprint .. v .. ",\r\n"
  15.         elseif (type(v) == "string") then
  16.             toprint = toprint .. "\"" .. v .. "\",\r\n"
  17.         elseif (type(v) == "table") then
  18.             toprint = toprint .. tprint(v, indent + 2) .. ",\r\n"
  19.         else
  20.             toprint = toprint .. "\"" .. tostring(v) .. "\",\r\n"
  21.         end
  22.     end
  23.     toprint = toprint .. string.rep(" ", indent-2) .. "}"
  24.     return(toprint)
  25. end
  26.  
  27. -- https://stackoverflow.com/a/2705804/22091473
  28. local function tlength(T)
  29.     local count = 0
  30.     for _ in pairs(T) do count = count + 1 end
  31.     return count
  32. end
  33.  
  34. -- https://stackoverflow.com/a/7615129/22091473
  35. local function split(str, separator)
  36.     if separator == nil then
  37.         separator = "%s"
  38.     end
  39.     local t={}
  40.     for str in string.gmatch(str, "([^"..separator.."]+)") do
  41.         table.insert(t, str)
  42.     end
  43.     return t
  44. end
  45.  
  46. return { tprint = tprint, tlength = tlength, split = split }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement