Operators
Arithmetic operators
There are several arithmetic operators that Lua offers out of the box. These are pretty common among computational tools (e.g. excel), so there might be nothing really new here for you.
And the result will be
Relational operators
In addition to add or substract, it will sometimes be relevant to compare values (not just numbers). In order to do this, Lua offers the relational operators. These operators always return true or false (i.e. a boolean) and never other kind of value. The available operators are:
Which results in
Have in mind that the ==
operator first compares the type of the variables. So, the expression 12 == "12"
will be false. As of how other operands work when variables are of different type, I do not know.
Logical operators
Logical operators are useful for evaluating more complex expressions than any relational operation allow. For example, 'A = true if B AND C are both true'
The relational operators in Lua are or
, and
and not
. These are pretty straightforward, as they are self-explanatory.
These operators can be nested and combined with relational operators, and used for defining new variables. For instance:
When assigning variables, it is relevant to consider which value do the three operators return. The Lua documentation states that:
The
not
operator always returnstrue
orfalse
.The
and
operator returns the first argument if it is equals tonil
orfalse
, otherwise it returns the second argumentThe
or
operator returns the first argument if this is different fromnil
orfalse
; otherwise, returns the second argument.
A convenient way of defining default values is the use of expressions such as v = some_value or 2
. This means "assign the value some_value
to v
. If some_value
is nil
(i.e. it has not been assigned), use the default value of 2 ".
Other operators
There are a few other operators that will be useful for Lua users in Emp. These are the length (#
), and concatenation (..
) operators.
The length operator, as its name states, usually returns the 'length' of the object that it is applied to. It can only be applied to Strings (returning the number of characters in it) and Tables (returning the last numeric key in it). A consecuence of the behavior of the length operator in Tables is that, when they are created without explicit keys (i.e. Arrays), it will return the real length of it.
This behavior may look a little strange and, in fact, I do not know the design reasons behind this. However, it is a reasonable behaviour as the length of a table tells you the maximum key you can use for getting a value (i.e. another_table[1]
does exist, while another_table[2]
does not).
The Concatenate operator puts together two variables of type String or Number into a single String.
Last updated