DekGenius.com
[ Team LiB ] Previous Section Next Section

15.3 Comparison Operators

The result is a boolean. The nature of comparisons involving strings can be influenced by a considering clause; see Chapter 12.

Lists are ordered, but records are not:

{1, 2} = {2, 1} -- false
{name:"Matt", age:"49"} = {age:"49", name:"Matt"} -- true

The equality (=) and inequality () operators do not coerce their operands; operands may be of any datatype, and operands of different datatypes are unequal. So, for example:

{"2"} = 2 -- false

The first operand is a list; the second operand is a number; no coercion takes place; therefore the operands are not equal, and the comparison is false.

With the other comparison operators, operands must 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 -- true

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
= (is)equality

Syntax

operand1 = operand2

Description

No coercion is performed; operands of different datatypes are not equal. Synonym is equal to has abbreviations equal, equals, and equal to.

This operator is not overloaded as an assignment operator; see Section 7.1. It is not an error for a line of AppleScript code to consist of an equality comparison, like this:

x = 3

That line is an equality comparison, not an assignment! If you write a line like that, intending to write an assignment, your code will generate incorrect results that can be difficult to track down.

(is not)inequality

Syntax

operand1 operand2

Description

No coercion is performed; operands of different datatypes are not equal. The not-equals sign is typed using Option-=. is not has abbreviation isn't. Synonym is not equal to has abbreviations is not equal, isn't equal, does not equal, and doesn't equal. There are no synonyms <> or !=.

<less than

Syntax

operand1 < operand2

Description

Synonyms are is less than (abbreviation less than) and comes before.

>greater than

Syntax

operand1 > operand2

Description

Synonyms are is greater than (abbreviation greater than) and comes after.

less than or equal to

Syntax

operand1 operand2

Description

Abbreviation is <=, or the symbol may be typed using Option-comma. Synonym is less than or equal to has abbreviations omitting is, to, or both. There are also synonyms does not come after and is not greater than.

greater than or equal to

Syntax

operand1 operand2

Description

Abbreviation is >=, or the symbol may be typed using Option-period. Synonym is greater than or equal to has abbreviations omitting is, to, or both. There are also synonyms does not come before and is not less than.

    [ Team LiB ] Previous Section Next Section