Lua/Expressions
< 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
editThis lesson assumes you have already completed the Variables lesson.
Create a Lua Script with Expressions
editTo create a Lua script with expressions:
- 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.arithmetic() local a = 3 local b = 2 local result result = ';Arithmetic\n' result = result .. ':a is ' .. a .. '\n' result = result .. ':b is ' .. 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' 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 is ' .. -a .. '\n' return result end function p.relational() local a = 3 local b = 2 local result result = ';Relational\n' result = result .. ':a is ' .. a .. '\n' result = result .. ':b is ' .. 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' 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 function p.logical() local a = 3 local b = 2 local result result = ';Logical\n' result = result .. ':a is ' .. a .. '\n' result = result .. ':b is ' .. 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' result = result .. ':a < b is ' .. tostring(a < b) .. '\n' result = result .. ':not (a < b) is ' .. tostring(not (a < b)) .. '\n' return result end function p.length() local string = 'This is a string' local result result = ';Length\n' result = result .. ':The length of "' .. string .. '" is ' .. #string 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|arithmetic}} {{#invoke:Sandbox|relational}} {{#invoke:Sandbox|logical}} {{#invoke:Sandbox|length}}
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
- 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
- Logical
- a is 3
- b is 2
- a < b and b < a is false
- a < b or b < a is true
- a < b is false
- not (a < b) is true
- Length
- The length of "This is a string" is 16
Understand Your Lua Script
editTo understand your Lua script:
local
and the following code defines the variables a, b, and result. a and b are initialized. result isnil
.3
and2
are numeric literals.';Arithmetic\n'
is a string literal. String literals may also be defined using double quotes, such as";Arithmetic\n"
.\n
is a newline character. Content that follows will appear on a new line in the resulting text...
is the concatenation operator. It appends two strings together. Numeric values are automatically converted to strings when concatenated.+
,-
,*
, and/
are add, subtract, multiply, and divide, respectively.%
is the modulo or remainder operator.^
is the exponentiation or 'raise to the power of' operator.-
preceding a variable is the negation operator.==
compares for equality.~=
compares for inequality.<
,>
,<=
, and>=
compare less than, greater than, less than or equal, and greater than or equal, respectively.tostring()
explicitly converts the content to a string. Logical comparisons do not automatically convert to strings.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.
- This left/right approach is more efficient, because
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.
- This left/right approach is more efficient, because
not
returns the true/false opposite of what follows.#
returns the length of the variable that follows.
Conclusion
editCongratulations! You've now created, tested, and understood a Lua script with expressions. Continue on to the Conditions lesson.