Module:Sandbox

-- This is a sandbox for testing lua scripts. -- See Extension:Scribunto for more information.

local p = {}

function show(key, value) r=r.. tostring(key) .. " = " .. value .. ", " end function p.f(frame)

r='' phone_book = { ["Sally Smart"] = "555-9999", ["John Doe"] = "555-1212", ["J. Random Hacker"] = "553-1337", -- Trailing comma is OK } aTable = { -- Table as value subTable = { 5, 7.5, k = true }, -- key is "subTable" -- Function as value ['John Doe'] = function (age) if age < 18 then return "Young" else return "Old!" end end, -- Table and function (and other types) can also be used as keys }

simple = { [true] = 1, [false] = 0, [3.14] = math.pi, x = 'x', ["!"] = 42 }

r=r..'\n\n'..phone_book["John Doe"] r=r..'\n\n'..aTable["John Doe"](45) r=r..'\n\n'..tostring(aTable.subTable.k)

-- Iterate on all keys r=r..'\n\n' for k, v in pairs(simple) do show(k, v) end r=r..'\n\n' k= nil repeat k, v = next(simple, k)       if k ~= nil then show(k, v) end until k == nil

-- An entry can be removed by setting it to nil:

simple.x = nil

-- Likewise, you can overwrite values or add them:

simple['%'] = "percent" simple['!'] = 111

r=r..'\n\n' for k, v in pairs(simple) do show(k, v) end

return r end return p