Jump to content

Lua/Expressions

From Wikiversity
< Lua

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] Expressions are comprised of literal values, variables, and arithmetic, relational and logical operators. This lesson will show you how to use expressions in your scripts.

Prerequisites

[edit | edit source]

This lesson assumes you have already completed the Variables lesson.

Create a Lua Script with Expressions

[edit | edit source]

To create a Lua script with expressions:

  1. Navigate to Module:Sandbox.
  2. Click on the menu Edit source and 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 do not save the page.

New Lua length function

[edit | edit source]
local p = {}
function p.length() -- get length of msg
    local msg = 'This is a string'
    local result = ';Length\n' -- Wiki semicolon ';' bolds the title.
    -- Operator sharp '#' returns the length of the variable msg that follows:
    result = result .. ':The length of "' .. msg .. '" is ' .. #msg
    return result
end
return p

Debug Console

[edit | edit source]

In Module:Sandbox, the above function p.length() -- get length of msg has been pasted:

  1. Scroll down until Debug console at the end of the page.
  2. Paste the following call in the gray input area (above Clear) and validate by Enter: ↲
print(p.length())

The result should be:

;Length
:The length of "This is a string" is 16

There is no need to save the Sandbox page to use the Debug console:

  1. Wikicode semicolon ';' -- bold, colon ':' -- left margin and HTML tag '<br>' -- line break are not rendered in the Debug console.
  2. Do not start a line with = to evaluate it as an expression without providing a target variable on the left:
    varTarget = expr -- assign expr (e.g. 40 + 2) to varTarget; result is 42
  3. Privilege: print(p.FunctionName())

Test Lua length function

[edit | edit source]

To test your Lua script:

  1. Save the Module:Sandbox to call it from another Wiki page.
  2. Navigate to either the Module_talk:Sandbox page, the Wikiversity:Sandbox page, or your own user or sandbox page.
  3. Add the following code and save the page:
{{#invoke:Sandbox|length}}

The result should be:

Length
The length of "This is a string" is 16

New Lua arithmetic function

[edit | edit source]

To create a Lua script with expressions:

  1. Add the following code and save the page.
local p = {}

function p.arithmetic()
    local a = 3
    local b = 2
    local result

    result = ';Arithmetic\n'
    result = result .. ':a is ' .. a .. '\n' -- Wiki colon ':' adds a left margin
    result = result .. ':b is ' .. b .. '\n'
    result = result .. ':a + b is ' .. a + b .. '\n' -- add
    result = result .. ':a - b is ' .. a - b .. '\n'
    result = result .. ':a * b is ' .. a * b .. '\n'
    result = result .. ':a / b is ' .. a / b .. '\n'
    result = result .. ':a % b is ' .. a % b .. '\n' -- modulo
    result = result .. ':a ^ b is ' .. a ^ b .. '\n' -- power
    result = result .. ':-a is ' .. -a .. '\n'

    return result
end

return p

Test Lua arithmetic function

[edit | edit source]

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|arithmetic}}

The result should be:

Arithmetic
a is 3
b is 2
a + b is 5
a - b is 1
a * b is 6
a / b is 1.5
a % b is 1
a ^ b is 9
-a is -3

New Lua relational function

[edit | edit source]
local p = {}
function p.relational()
    local a = 3; local b = 2; local result = ';Relational\n'
    result = result .. ':a is ' .. a .. '\n' .. ':b is ' .. b .. '\n'
    result = result .. ':a == b is ' .. tostring(a == b) .. '\n' -- equal
    result = result .. ':a ~= b is ' .. tostring(a ~= b) .. '\n' -- different
    result = result .. ':a < b is ' .. tostring(a < b) .. '\n'
    result = result .. ':a > b is ' .. tostring(a > b) .. '\n'
    result = result .. ':a <= b is ' .. tostring(a <= b) .. '\n'
    result = result .. ':a >= b is ' .. tostring(a >= b) .. '\n'
    return result
end
return p

Test Lua relational function

[edit | edit source]

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|relational}}

The result should be:

Relational
a is 3
b is 2
a == b is false
a ~= b is true
a < b is false
a > b is true
a <= b is false
a >= b is true

New Lua logical function

[edit | edit source]
local p = {}
function p.logical()
    local a = 3; local b = 2; local result = ';Logical\n'
    result = result .. ':a is ' .. a .. '\n' .. ':b is ' .. b .. '\n'
    result = result .. ':a < b is ' .. tostring(a < b) .. '\n'
    result = result .. ':not (a < b) is ' .. tostring(not (a < b)) .. '\n'
    result = result .. ':a < b and b < a is ' .. tostring(a < b and b < a) .. '\n'
    result = result .. ':a < b or b < a is ' .. tostring(a < b or b < a) .. '\n'
    return result
end
return p

Test Lua logical function

[edit | edit source]

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|logical}}

The result should be:

Logical
a is 3
b is 2
a < b is false
not (a < b) is true
a < b and b < a is false
a < b or b < a is true

Understand Your Lua Script

[edit | edit source]

To understand your Lua script:

Length
  1. # returns the length of the variable that follows.
Arithmetic
  1. local and the following code defines the variables a, b, and result. a and b are initialized. result is nil.
  2. 3 and 2 are numeric literals.
  3. ';Arithmetic\n' is a string literal. String literals may also be defined using double quotes, such as ";Arithmetic\n".
  4. '\n' is a newline character. Content that follows will appear on a new line in the resulting text.
  5. .. is the concatenation operator. It appends two strings together. Numeric values are automatically converted to strings when concatenated.
  6. +, -, *, and / are add, subtract, multiply, and divide, respectively.
  7. % is the modulo or remainder operator.
  8. ^ is the exponentiation or 'raise to the power of' operator.
  9. - preceding a variable is the negation operator.
Relational
  1. == compares for equality.
  2. ~= compares for inequality.
  3. <, >, <=, and >= compare less than, greater than, less than or equal, and greater than or equal, respectively.
  4. tostring() explicitly converts the content to a string. Logical comparisons do not automatically convert to strings.
Logical
  1. and returns false if the left operand is false, or the value of the right operand if the left operand is true.
    This left/right approach is more efficient, because and stops evaluating as soon as it knows the result is false.
  2. or returns true if the left operand is true, or the value of the right operand if the left operand is false.
    This left/right approach is more efficient, because or stops evaluating as soon as it knows the result is true.
  3. not returns the true/false opposite of what follows.

Conclusion

[edit | edit source]

Congratulations! You've now created, tested, and understood a Lua script with expressions. Continue on to the Conditions lesson.

See Also

[edit | edit source]

References

[edit | edit source]
  1. "Extension:Scribunto/Lua reference manual". Mediawiki. 2026. Retrieved 2026-04-04.