Comparison Operators
The comparison operators test whether one operand is the same as the other, or whether, if they can be ordered, they are ordered in a given direction. The result is a boolean.
The nature of comparisons involving strings can be influenced by a considering clause; see Chapter 19.
Lists are internally ordered, but records are not:
{1, 2} = {2, 1} -- false
{name:"Matt", age:"51"} = {age:"51", name:"Matt"} -- trueThe equality (=) and inequality (≠) operators do not coerce their operands; operands may be of any datatype, and operands of different datatypes are unequal. So:
{"2"} = 2 -- falseIn that example, the first operand is a list, the second operand is a number, and no coercion takes place. So the operands are not equal, and the comparison is false.
With the other comparison operators , operands must ultimately be a string, a number, or a date. The first operator is coerced to a string, a number, or a date, and then the second operator is coerced to match the datatype of the first:
{"2"} ≥ 2 -- trueIn that example, the first operand is a list of one string, so it is coerced to a string. Now the second operand is coerced to a string; the two strings are equal and the comparison is true.
Thus, although you cannot use the equality operator to learn whether two values would be equal if implicitly coerced to the same datatype, you can work around the problem like this:
{"2"} ≤ 2 and {"2"} ≥ 2 -- trueAs noted in "Assignment and Retrieval" in Chapter 7, the equality operator ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access