Module:Sandbox

From Wikiversity
Jump to navigation Jump to search

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