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

Contenu supprimé Contenu ajouté
Youni Verciti (discussion | contributions)
Youni Verciti (discussion | contributions)
Ligne 66 :
 
== Comprendre le nouveau script ==
ToPour understandcomprendre yourles Lua scriptfonctions <code>tablelibrary</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>local numbers = {}</code> definesdéfinit an emptyune table variablevide namednommé <code>numbers</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.
# <code>math.randomseed(os.time())</code> ...
 
# <code>math.randomseed(os.time())</code> seeds the Lua random number generator with the current server operating system elapsed time in seconds.
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.
#* If the random number generator is not seeded, it will return the same sequence of numbers every time.
#* The MediaWiki software caches page results. It is necessary to purge the server page cache to see new results. See {{tlxm|Purge}} for more information.
# <code>for i = 1, 10, 1 do</code> createscrée aune loopboucle code block thatqui willse repeatrepetera <code>10</code> timesfois.
# <code>table.insert(numbers, math.random(1, 10))</code> callsappel thela fonction <code>mathrandom</code> libraryde la librairie <code>randommath</code> functionpour torécupérer retrieveun anombre randomaléatoire numberentre between <code>1</code> andet <code>10</code>, inclusiveinclus, andet theninsert insertscette thatvaleur valuedans intole thechamps <code>numbers</code> de la table.
# <code>table.concat(numbers, ', ')</code> retrievesrécupère thetoutes valuesles indonnées thede la table <code>numbers</code> andpour convertsles themconvertir intoen achaine stringde concatenatedcaractères withconcaténée avec le séparateur <code>', '</code> separators betweenentre eachchaque valuevaleur.
# <code>table.sort(numbers)</code> sortstri thela table <code>numbers</code> tabledans l'ordre croissant, inordre thede defaulttri ascendingpar orderdéfaut.
# <code>table.remove(numbers, 1)</code> removessupprime thel'élément elementà inla position <code>1</code> fromde thela table.
#: SinceDans thela tablemesure wasoù la table sortedest priortriée toau removalpréalable, thisnous willsupprimons removela theplus lowestpetite valuevaleur fromde thela table.
# <code>table.sort(numbers, function(a, b) return a > b end)</code> tri la table <code>numbers</code> dans l'ordre décroissant. La fonction <code>table.sort</code> attend comme second argument optionnel une comparaison obtenue par le biais d'une fonction <code> function(a, b) return a > b end </code>. Cette fonction est une comparaison logique qui vraie lorsque le premier argument est supérieur au second.
# <code>table.sort(numbers, descending)</code> sorts the table using a custom <code>descending</code> comparison function.
#: L'emploi d'une fonction de comparaison logique comme second argument, autorise toute sorte de tri sur des tables complexes , retenez <code>function(a, b) return a > b end)</code> permet d'inverser l'ordre de tri.
 
 
=== to remove ===
 
== Conclusion ==