Operators

The operators used in an expression are evaluated in the following order of precedence:

  • Operations inside brackets,
  • then power,
  • then multiplication/division,
  • then addition/subtraction.

The following operators may be used in an expression:

Arithmetic operators

Operator Effect for numbers Effect for strings Effect for Boolean
+ Addition Concatenation Logical OR
- Subtraction Invalid operation Logical OR inverted
* Multiplication Invalid operation Logical AND
/ Division Invalid operation Logical AND inverted
^ Power *1 *
% Remainder1 Invalid operation *

1The % (Remainder) operator calculates the remainder after division. For example, 12 % 10 is 2. At school you might have said it as “12 divided by 10 is 1, with remainder 2". Similarly, 20 % 10 is zero because there is no remainder.

Comparison operators

Sign Operator
= Equal
!= Not equal
== Equivalent (see "Equivalence" below)
< Less than
<= Less or equal
> Greater than
>= Greater or equal
A<x<B (see "Range checks" below)

Equivalence

Using the equivalence (==) operator, equivalent values are expected to have the same type and be equal. This example returns False:

"42" == 42

Using the equals (=) operator for the same expression, on the other hand, returns True:

"42" = 42

With the exception of the equivalence operator, if the types of the left and right expressions are different, the right expression will be converted to the type of left expression before making the comparison. Comparison operations produce a Boolean result.

Range checks

The following range checks are supported:

(expr) (comparison operator1) (expr) (comparison operator2) (expr)

Where both comparison operators are less than (or equal), or greater than (or equal), but not both.

Less than or equal

a<b<c
for example: 2<x<3 returns true if x is greater than 2 but less than 3

a<=b<c
for example: 2<=x<3 returns true if x is greater than or equal to 2 but less than 3

a<b<=c

for example: 2<x<=3 returns true if x is greater than 2 but less than or equal to 3

a<=b<=c
for example: 2<=x<=3 returns true if x is greater than or equal to 2 and less than or equal to 3

Greater than or equal

a>b>c
for example: 2>x>3 returns true if x is less than 2 but greater than 3

a>=b>c
for example: 2>=x>3 returns true if x is less than or equal to 2 but greater than 3

a>b>=c
for example: 2>x>=3 returns true if x is less than 2 but greater than or equal to 3

a>=b>=c
for example: 2>=x>=3 returns true if x is less than or equal to 2 and greater than or equal to 3

The operator has the same precedence as normal comparison operators, so:

2<=2+1<4

evaluates to True.

Logic operators

Sign Operator
& Logical AND
| Logical OR
! Logical NOT

In the following example the Logical OR (pipe) operator is used in a field expression to define a filter. If a record contains either 1, 2 or 3 in its BLOCK field, then the record passes the filter:

=[BLOCK] = "1" | [BLOCK] = "2" | [BLOCK] = "3"

The same expression can be written more succinctly using the IN function:

=IN([BLOCK], "1", "2", "3")

Conditional Expressions

The expression parser has a conditional operator 'if'. It has the following syntax:

if <predicate> then
    <expression_true>
elif | elsif | elseif <predicate> then
    <expression_true>
else
    <expression_false>
endif | fi

The elif | elsif | elseif part of the operator can be repeated, or be skipped completely. The logic is to evaluate predicates until the one that evaluates to TRUE is found, and then evaluate the corresponding <expression_true>. If no predicate evaluates to TRUE, then <expression_false> is evaluated and this becomes the result.

You can use the 'if' operator in the following way:

MCAF + if RECOVERY > 0.5 then MCAF * 2 else 0 fi

In other words, if the RECOVERY is greater than 0.5, then add MCAF * 2 to MCAF, otherwise add 0 (nothing).

For more information, see: Conditional Expressions

Type conversions

Type Numeric String Boolean
Numeric - String containing the numeric value, i.e. “5" for 5 -
String If possible, numeric value from the string. Otherwise NIL. - -
Boolean 1, if TRUE. 0, if FALSE. “1" if TRUE. “0" if FALSE. -

Boolean conversion rules

When writing Boolean {TRUE} or {FALSE} expression results to a file, {TRUE} will be implicitly converted to the number 1 or the character "1", and {FALSE} will be implicitly converted to the number 0 or the character "0", depending on the destination field type.

There is no corresponding implicit conversion from numerical or character values to Boolean when reading from a file, so you must explicitly write the conversion yourself.

Type conversion operators

The result type of an expression is determined by the type of the first operand. However, this is not always convenient since something like:

=[RL] + "m"

will give an unexpected result.

A unary operator “#" that can be used to convert an operand, clause, or expression to a string is handy in these situations. So:

=#[RL] + "m"

gives the correct result.

A few examples are shown in the following table:

=3 + 4 produces the number 7, whereas:
=#(3 + 4) produces the text “7", and:
=#3 + 4 produces the text “34", which is simply the concatenation of 3 and 4.

Unless otherwise stated, the type of the right expression will be converted to the type of the left expression before executing the operation.

i.e. "5.05" will be the (concatenated) result of “5.0" + 5, and 10 will be the (arithmetic) result of 5.0 + “5".

Literal constants take precedence when determining the context type. For example:

=2 + "3"

evaluates to 5, but if A is a variable with value 2,

=A + “3”

evaluates to 23.