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.[1] This lesson will show you how to use the Lua Table library in your scripts.
Prerequisites
editThis lesson assumes you have already completed the Tables lesson.
Create a Lua Script that Uses the Table Library
editTo 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:
local p = {} function p.tablelibrary() local numbers = {} local result math.randomseed(os.time()) for i = 1, 10, 1 do table.insert(numbers, math.random(1, 10)) end result = ';random numbers\n' result = result .. ':' .. table.concat(numbers, ', ') .. '\n' table.sort(numbers) result = result .. ';sorted numbers\n' result = result .. ':' .. table.concat(numbers, ', ') .. '\n' table.remove(numbers, 1) result = result .. ';after removing the first value\n' result = result .. ':' .. table.concat(numbers, ', ') .. '\n' table.sort(numbers, function(a, b) return a > b end) result = result .. ';sorted in descending order\n' result = result .. ':' .. table.concat(numbers, ', ') .. '\n' return result end return p
Test Your Lua Script
editTo 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:
{{#invoke:Sandbox|tablelibrary}}
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
editTo understand your Lua script descending
function:
local function descending(first, second)
declares a local function nameddescending
that accepts two parameters,first
andsecond
.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 namednumbers
.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 repeat10
times.table.insert(numbers, math.random(1, 10))
calls themath
libraryrandom
function to retrieve a random number between1
and10
, inclusive, and then inserts that value into thenumbers
table.table.concat(numbers, ', ')
retrieves the values in the tablenumbers
and converts them into a string concatenated with', '
separators between each value.table.sort(numbers)
sorts thenumbers
table in the default ascending order.table.remove(numbers, 1)
removes the element in position1
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 customdescending
comparison function.
Conclusion
editCongratulations! 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.