« Introduction au Lua/Fonctions Table » : différence entre les versions

Contenu supprimé Contenu ajouté
Ligne 12 :
#: It's a sandbox. Everyone is free to play in the sandbox. But if you find another user is actively editing the sandbox at the same time, you may also use Module:Sandbox/Username, where Username is your Wikiversity username.
# Add the following code and save the page:
 
 
<blockquote><pre>
local p = {}
 
local function tableToStringp.tablelibrary(t)
local key
local value
local result
result = ''
for key, value in pairs(t) do
result = result .. ':table[' .. key .. '] is ' .. value .. '\n'
end
return result
end
function p.sort()
local numbers = {}
local result
math.randomseed(os.time())
for i = 1, 510, 1 do
table.insert(numbers, math.random(01, 10))
end
result = ';random numbers\n'
result = result .. tableToString':' .. table.concat(numbers, ', ') .. '\n'
table.sort(numbers)
result = result .. ';sorted numbers\n'
result = result .. tableToString':' .. table.concat(numbers, ', ') .. '\n'
 
table.remove(numbers, 1)
result = result .. ';after removing the first value\n'
result = result .. ':table[' .. key .table.concat(numbers, '] is, ' .. value) .. '\n'
 
return result
end