# Lua/Table Library

Lua modules based on the Scribunto/Lua extension are stored in resource pages using the Module: namespace. Each module uses a table to hold functions and variables, and that containing table is returned at the end of the module code. This lesson will show you how to use the Lua Table library in your scripts.

## Prerequisites

This lesson assumes you have already completed the Tables lesson.

## Create a Lua Script that Uses the Table Library

To create a Lua script that uses the Table library:
* Navigate to Module:Sandbox.
* Clear all existing code.
    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:

## Test Your Lua Script

To test your Lua script:
* Navigate to either the Module_talk:Sandbox page, the Wikiversity:Sandbox page, or your own user or sandbox page.
* Add the following code and save the page:
The result should be similar to:

   ### random numbers

       2, 7, 5, 7, 5, 4, 9, 1, 10, 4

   ### sorted numbers

       1, 2, 4, 4, 5, 5, 7, 7, 9, 10

   ### after removing the first value

       2, 4, 4, 5, 5, 7, 7, 9, 10

   ### sorted in descending order

       10, 9, 7, 7, 5, 5, 4, 4, 2

## Understand Your Lua Script

To understand your Lua script descending function:
* local function descending(first, second) declares a local function named descending that accepts two parameters, first and second.
* return (first > second) 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 tablelibrary function:
* local numbers = defines an empty table variable named numbers.
* math.randomseed(os.time) 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 {{Purge}} for more information.
* for i = 1, 10, 1 do creates a loop code block that will repeat 10 times.
* table.insert(numbers, math.random(1, 10)) calls the math library random function to retrieve a random number between 1 and 10, inclusive, and then inserts that value into the numbers table.
* table.concat(numbers, ', ') retrieves the values in the table numbers and converts them into a string concatenated with ', ' separators between each value.
* table.sort(numbers) sorts the numbers table in the default ascending order.
* table.remove(numbers, 1) removes the element in position 1 from the table.
    Since the table was sorted prior to removal, this will remove the lowest value from the table.
* table.sort(numbers, descending) sorts the table using a custom descending comparison function.

## Conclusion

Congratulations! You've now created, tested, and understood a Lua script that uses the Table library. Return to the main Lua page to learn about other Lua code libraries.

## See Also

* Lua Tables