Module:SandboxTables

From Rhizome Artbase
sequence
table[1] is 10
table[2] is 20
table[3] is 30
dictionary
table['es'] is Spanish
table['ja'] is Japanese
table['fr'] is French
table['ru'] is Russian
table['de'] is German
table['ko'] is Korean
table['en'] is English
table['zh'] is Chinese
table['it'] is Italian

local p = {}
 
local function tableToString(t)
    local key
    local value
    local result
 
    result = ''
 
    for key, value in pairs(t) do
        if (tonumber(key) ~= nil) then
            result = result .. ':table[' .. key .. '] is ' .. value .. '\n' 
        else
            result = result .. ':table[\'' .. key .. '\'] is ' .. value .. '\n' 
        end
    end
 
    return result
end
 
function p.sequence()
    local numbers = {10, 20, 30}
    local result
 
    result = ';sequence\n'
    result = result .. tableToString(numbers)
 
    return result
end
 
function p.dictionary()
    local languages = {
        ['de'] = 'German',
        ['en'] = 'English', 
        ['es'] = 'Spanish', 
        ['fr'] = 'French',
        ['it'] = 'Italian',
        ['ja'] = 'Japanese',
        ['ko'] = 'Korean',
        ['ru'] = 'Russian',
        ['zh'] = 'Chinese'
    }
    local result
 
    result = ';dictionary\n'
    result = result .. tableToString(languages)
 
    return result
end
 
return p