User Tools

Site Tools


lgscript:lua_operators

Operators

Operators modify the values from variables and function calls to form expressions. The simplest expressions don’t need any operators. There are different operators for table values, strings, numbers, and booleans. There are also comparison operators, the list operators comma and semi-colon, and grouping parenthesis.

Operators are evaluated in a predictable order. From highest to lowest precedence:

Grouping ( )
Tables { } [ ] . :
Numbers ^
Tables #
Boolean not
Numbers - (negative)
Numbers * / %
Numbers + -
Strings ..
Comparison == ~= <= >= < >
Boolean and
Boolean or
List , ;

Grouping part of an expression in parenthesis will cause those inside part of the expression to be evaluated before those on either side.

Operators with values on the left and right side will evaluate the left side first. Except for the exponent (^) and concatenation (..) operators; those two evaluate the right side before the left.

Table Operators

{ list } Creates a new table using the values in the list.
table [ index ] Refers to the value of a table index.
table . name Refers to the value of a table field.
table : name Refers to a method in the table.
# table This is called the “length” operator, but isn’t really.

String Operators

left .. right Concatenation operator. Joins two strings.

Concatenation of numbers is allowed. The value is converted to a decimal representation of the number then joined as a string.

Object types can support concatenation through metamethods.

Number Operators

number ^ exponent Exponent operator.
- number Negates the number.
num % den Modulo operator. Returns the integer remainder when num is divided by den.
num / den Division operator. The result may be a real number even if both numbers are integers.
left * right Multiplication operator.
left - right Subtraction operator.
left + right Addition operator.

Object types can support arithmetic through metamethods.

Comparison Operators

left == right Equal to.
left ~= right Not equal to.
left <= right Less than or equal to.
left >= right Greater than or equal to.
left < right Less than.
left > right Greater than.

Integer and real numbers can be compared with each other. For all other values, the types must be the same in order to make a comparison. Two different types can be compared using == or ~=, and they will always be not equal. Attempting to compare different types as less than or greater than is an error. Object types are equal if they reference the same object. Comparison of object types can be customized with metamethods.

A common mistake when starting out with Lua is to write “not equal” as either != or <>, because those operators are common in other languages.

Boolean Operators

not value Contradiction.
first and second Conjunction.
first or second Alternation.

The not operator has a very high precedence, so using parenthesis will often be required with it. If the value evaluates to nil or false, then the result of the operator is true. Otherwise it is false.

The and and or operators will evaluate the values as booleans, but will not convert them. So the result is either the first or second value. In some cases, only the first value is sufficient to complete the operation. For the and operator, if the first value is false, then the logical result of the operation will always be false. If the first value to the or operator is true, then the result is always true. When these cases occur, the operators will not evaluate of the second value. This means that any side-effects of getting the value, such as calling a function, will also not occur. It also means that while the second value is being evaluated, then the condition tested by the first value will be false or true depending on the operator used.

The logical operators are useful for creating simple conditional expressions without having to create an if-then-else block. This is most often helpful when testing for a nil value.

function get(directory, name)
    -- If the name is not given, use a default name
    name = name or "default"
    local table = directory[name]
    -- Only try retrieving the value if the table exists
    return table and table.value
end
 
-- The long way
function get(directory, name)
    if not name then name = "default" end
    local table = directory[name]
    if table then
        return table.value
    else
        return nil
    end
end
lgscript/lua_operators.txt · Last modified: 2009/09/01 00:36 by telliamed