Lua/Table Library

From Wikiversity
< Lua
Jump to navigation Jump to search

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[edit]

This lesson assumes you have already completed the Tables lesson.

Create a Lua Script that Uses the Table Library[edit]

To create a Lua script that uses the Table library:

  1. Navigate to Module:Sandbox.
  2. 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.
  3. 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[edit]

To test your Lua script:

  1. Navigate to either the Module_talk:Sandbox page, the Wikiversity:Sandbox page, or your own user or sandbox page.
  2. 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[edit]

To understand your Lua script descending function:

  1. local function descending(first, second) declares a local function named descending that accepts two parameters, first and second.
  2. 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:

  1. local numbers = {} defines an empty table variable named numbers.
  2. 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.
  3. for i = 1, 10, 1 do creates a loop code block that will repeat 10 times.
  4. 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.
  5. table.concat(numbers, ', ') retrieves the values in the table numbers and converts them into a string concatenated with ', ' separators between each value.
  6. table.sort(numbers) sorts the numbers table in the default ascending order.
  7. 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.
  8. table.sort(numbers, descending) sorts the table using a custom descending comparison function.

Conclusion[edit]

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[edit]

References[edit]