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

Contenu supprimé Contenu ajouté
Ligne 62 :
 
== Understand Your Lua Script ==
To understand your Lua script <code>descending</code> function:
# <code>local function descending(first, second)</code> declares a local function named <code>descending</code> that accepts two parameters, <code>first</code> and <code>second</code>.
# <code>return (first > second)</code> returns true if the first is greater than the second, which will result in values being sorted in descending order.
 
To understand your Lua script <code>tablelibrary</code> function:
# <code>local numbers = {}</code> defines an empty table variable named <code>numbers</code>.
# <code>math.randomseed(os.time())</code> seeds the Lua random number generator with the current server operating system elapsed time in seconds.
Ligne 70 ⟶ 74 :
# <code>table.insert(numbers, math.random(1, 10))</code> calls the <code>math</code> library <code>random</code> function to retrieve a random number between <code>1</code> and <code>10</code> and then inserts that value into the <code>numbers</code> table.
# <code>table.concat(numbers, ', ')</code> retrieves the values in the table <code>numbers</code> and converts them into a string concatenated with <code>', '</code> separators between each value.
# <code>table.sort(numbers)</code> sorts the <code>numbers</code> table in the default ascending order.
# <code>table.remove(numbers, 1)</code> removes the element in position <code>1</code> from the table.
#: Since the table was sorted prior to removal, this will remove the lowest value from the table.
# <code>table.sort(numbers, descending)</code> sorts the table using a custom <code>descending</code> comparison function.
 
== Conclusion ==