Re-learning Lua
Lua - Basics for x = 0, 10, 1 do print(x) end -- for loop with range and step. step if not given is assumed to be 1 -- Tables are the soul of Lua. They double up as arrays and hashes a = {} b = {1, 2, 3} b[1] -- b[1] will return 1 as indexing for arrays is from 1 c = {[0]={123,3},["a"]={1,2,3},v={2,6,7}} -- We can have a "0"th index by adding the 1st element as [0]=value -- Adding keys which are not valid identifiers in Lua needs to be done by wrapping key in [] -- If a key is a valid identifier, we can access using `.` e.g c.a for above code vs c[0] for k,v in pairs(c) do print(type(k)) end -- iterates through k/v in table and prints type of key Print a table as valid Lua code function printTable(tbl) -- Specify function vars as local else they become global local s = "{" local fst = true for k,v in pairs(tbl) do local tk = type(k) local tv = type(v) if not fst then s = s .. "," else fst = false end if tk == "number" or tk == "boolean" then s = s .. "[" .. tostring(k) .. "]=" else s = s .. '["' .. k .. '"]=' end if tv == "table" then s = s .. printTable(v) elseif tv == "number" or tv == "boolean" then s = s .. tostring(v) elseif tv == "string" then s = s .. '"' .. v .. '"' else s = s .. vt end end s = s .. "}" return s end sv = printTable({[0]={123,3},["a"]={1,2,3},v={2,6,7},[true]=123}) print(sv) -- posts.lua (saved table) can be read from another lua file as posts=require("posts") return {[0]={[1]=123,[2]=3},["v"]={[1]=2,[2]=6,[3]=7},["a"]={[1]=1,[2]=2,[3]=3},[true]=123}