Relational operators are supplied which return the Boolean values true or false (or equivalent logical values for matrices).
== equal to
~= not equal to
< less than
> greater than
<= less than or equal to
>= greater than or equal to
Examples:
> print(1 == 1, 1 == 0)
true false
> print(1 ~= 1, 1 ~= 0)
false true
> print(2 < 7, 2 > 7)
true false
> print(3 <= 7, 7 <= 7, 8 <= 7)
true true false
> print(3 >= 7, 7 >= 7, 8 >= 7)
false true true
These also work on strings and other types.
> print("abc" < "def")
true
> print("abc" > "def")
false
> print("abc" == "abc")
true
> print("abc" == "a"+"bc")
true
Objects are not equal if the types are different or refer to different objects.
> print({} == "table")
false
> print({} == {}) // two different tables are created here
false
> t = {}
> t2 = t
> print(t == t2) // we're referencing the same table here
true
Coercion does not work here - the types must be converted explicitly. See the Coercion topic for more information.
> print("10" == 10)
false
> print(tonumber("10") == 10)
true
For the matrix data type, the operation is performed on an element-by-element basis and a matrix of Boolean is returned.
> A = [1,2]; B = [1,3]
> print(A == B)
[Matrix] 1 x 2
1 0
> print(A ~= B)
[Matrix] 1 x 2
0 1