### Reference: Expressions in programmed merges

Merge expressions are used to determine values in the ASSIGN, ASSIGNLOCAL, CAPS, CASEOF, CTON, FIRSTCAP, FOREACH, FORNEXT, IF, NTOC, STRLEN, STRPOS, SUBSTR, SWITCH, TOLOWER, TOUPPER, and WHILE commands. Expressions can perform operations on either numbers or strings.

The following topics explain how to use expressions when you program a merge in WordPerfect.

#### Understanding expression-related terms The following are technical terms used in the discussion of expressions. Understanding these terms is not essential to performing merges. These definitions are provided for those who are familiar with basic programming concepts.

 •
 •
 •
 •
 •
 •
 •
 •
 •
 •

##### Bitwise operation

In computer language, numbers are represented as a series of 16 bits. A bit can only have a value of 1 or 0. Each 16-bit series (or column) represents a numeric value (such as 1, 2, 4, 8 and so on). The following table outlines some values and their corresponding bits.

Value
Bits
0
0000000000000000
–1
1111111111111111
–3
1111111111111101
–21
1111111111101011
3
0000000000000011
4
0000000000000100
7
0000000000000111
21
0000000000010101
47
0000000000101111

A bitwise operation works on one column at a time (starting with the right column) using a single bit from each number. The operation is done 16 times so each bit of each number is operated on.

##### Bitwise AND (&)

A bitwise AND operation compares the bits of two numbers. When both numbers have a 1 bit in the same position (for example, there is a 1 bit in column 1 [the right column] of the first number and the second number), a 1 is placed in that position in the result. The following table demonstrates the way the expression 21&47 is evaluated.

Value
Bits
21
0000000000010101
47
0000000000101111
21&47
0000000000000101

The resulting bits represent the number 5, so 21&47=5.

##### Bitwise NOT (!)

A bitwise NOT operation takes the bits of the number and complements them. For example, if the expression is !0 (0 is 0000000000000000), the resulting value is –1 (–1 is 1111111111111111).

##### Bitwise OR (|)

A bitwise OR operation compares the bits of both numbers. When either number has a 1 bit in the same position (for example, there is a 1 bit in column 1 [the right column] of the first number or the second number), a 1 is placed in that position in the result. For example, the expression 21|47 is evaluated as shown in the following table:

Value
Bits
21
0000000000010101
47
0000000000101111
21|47
0000000000111111

The resulting bits represent the number 63, so 21|47=63.

##### Evaluate the expression

Perform the operation(s) on the expression.

##### Exclusive OR (XOR)

A logical XOR operation evaluates two expressions and returns a true (–1) if one, but not both, of the expressions is true. If both expressions are false or both are true, XOR returns a false (0). For example, 1=1 XOR 2=3 is true (–1), because only one of the expressions being evaluated is true. 6>3 XOR 3>1 is false (0), because both expressions are true.

##### Logical AND (AND)

A logical AND operation evaluates two expressions and returns a true (–1) if both expressions are true. In all other cases, it returns a false (0). For example, 1=1 AND 2=3 is false (0), because one of the expressions being evaluated is false. 6>3 AND 3>1 is true (–1), because both expressions are true.

##### Logical NOT (NOT)

A logical NOT operation returns the logical opposite (0 or –1) of the expression. WordPerfect interprets any nonzero numeric value as true, but when it assigns a value for true, it assigns –1. For example, NOT 2 is false (0), because 2 is evaluated as true. NOT 0 is true (–1).

##### Logical OR (OR)

A logical OR operation evaluates two expressions and returns a true (–1) if either or both expressions are true. If both expressions are false, it returns a false (0). For example, 1=1 OR 2=3 is true (–1), because one of the expressions being evaluated is true. 6<3 OR 3<1 is false (0), because both expressions are false.

##### Negative numbers

In WordPerfect, negative numbers are represented as large positive numbers, from 2,147,483,648 to 4,294,967,295. The number

4,294,967,295 is –1, 4,294,967,294 is –2, and so on. To determine the number WordPerfect uses to represent any given negative number from –1 to –2,147,483,648, use the following formula:

`  4,294,967,296 – |x|   `

where x is the negative number whose equivalent you are trying to find. For example, to find the equivalent of –3,

`  4,294,967,296 – 3 = 4,294,967,293   `

To find the negative number (n) represented by a given equivalent, use this formula:

`  x – 4,294,967,296=n   `

where x is the equivalent. For example, to find the negative number represented by 4,294,967,293,

`  4,294,967,293 – 4,294,967,296 = –3   `

You can assign variables to be negative numbers by using the minus (–) operator or by using the WordPerfect equivalent. Do not use commas or other punctuation in the equivalent. For example,

`  ASSIGN(number;–1)   `

is the same as

`  ASSIGN(number;4294967295)   `

##### WordPerfect character set values

WordPerfect assigns a unique value to each character in each WordPerfect character set. For more information, see the descriptions for the CTON and NTOC commands under “Reference: List of merge programming commands.” This is called the WordPerfect character set value. In a string comparison, the character set values are compared.

For characters in the same character set, one character is considered “less than” another character if the first character comes before the second character. For example, in character set 0, “3” is less than “4” and “A” is less than “a.” For characters in different character sets, the character from the character set with the lower numerical value is considered “less than” the character from the higher numerical character set. For example, any character from character set 2 is less than any character from character set 3.

#### Using numeric expressions Numeric expression values must contain only integers (or variables that contain integers).

You can use signed numbers in expressions. The highest positive number you can use is 2,147,483,647. Numbers higher than that are considered negative in WordPerfect. For information, see “Negative numbers.” The lowest negative number you can use is –2,147,483,648.

If you try to use an invalid numeric expression, the expression is treated as a text string. Examples of invalid numeric expressions are characters other than numbers and valid operators or expressions that evaluate to numbers outside the numeric limits.

The following is a list of numeric expressions. In this list, the terms n1 and n2 represent number 1 and number 2. Although only a single operator is illustrated in each example
below, you can use several operators as well as parentheses in expressions. For definitions of the operations used in the following table, see “Understanding expression-related terms.”

Expression
Operation
NOT n1
Returns the logical (0 or –1) NOT of n1.
Examples: NOT 0 is –1. NOT 2 is 0.
!n1
Returns the arithmetic (bitwise) NOT of n1.
Examples: !0 is –1. !2 is –3.
-n1
Returns the negative of n1.
Example: If variable Num holds 5, –VARIABLE(Num) is –5.
n1+n2
Returns the sum of n1 and n2.
Example: 5+4 is 9.
n1–n2
Returns the difference of n1 and n2.
Example: 10–1 is 9.
n1*n2
Returns the product of n1 and n2.
Example: 6*5 is 30.
n1/n2 or n1 DIV n2
Returns the integer quotient of n1 and n2.
Examples: 20/5 is 4. 5 DIV 2 is 2.
n1%n2 or n1 MOD n2
Returns the remainder of the quotient of n1 and n2.
Examples: 20%5 is 0. 5 MOD 2 is 1.
n1 AND n2
Returns the logical (0 or –1) AND of n1 and n2.
Examples: 0 AND –1 is 0. 3 AND 4 is –1.
n1 & n2
Returns the arithmetic (bitwise) AND of n1 and n2.
Examples: 7&4 is 4. 3&4 is 0.
n1 OR n2
Returns the logical (0 or –1) OR of n1 and n2.
Examples: 0 OR –1 is –1. 3 OR 4 is –1.
n1|n2
Returns the arithmetic (bitwise) OR of n1 and n2.
Examples: 7|4 is 7. 3|4 is 7.
n1 XOR n2
Returns the logical (0 or –1) XOR (exclusive OR) of n1 and n2.
Examples: 0 XOR –1 is –1. 3 XOR 4 is 0.
n1=n2
Returns a true value (–1) if n1 and n2 are equal; otherwise, returns a false value (0).
Examples: If variable 1 holds 5, then VARIABLE(1)=5 is true, and VARIABLE(1)=3 is false.
n1!=n2 or n1<>n2
Returns a true value (–1) if n1 and n2 are not equal; otherwise, returns a false value (0).
Examples: If variable 1 holds 5, then VARIABLE(1)!=3 is true, and VARIABLE(1)<>5 is false.
n1>n2
Returns a true value (–1) if n1 is greater than n2; otherwise, returns a false value (0).
Examples: 6>4 is true. 4>6 is false.
n1<n2
Returns a true value (–1) if n1 is less than n2; otherwise, returns a false value (0).
Examples:2<10 is true. 10<2 is false.
n1>=n2
Returns a true value (-1) if n1 is greater than or equal to n2; otherwise, returns a false value (0).
Examples: 6>=6 is true. 4>=6 is false.
n1<=n2
Returns a true value (–1) if n1 is less than or equal to n2; otherwise, returns a false value (0).
Examples: 2<=2 is true. 10<=2 is false.

#### Using string expressions A string is any sequence of one or more characters, including spaces. For example, 245, Strawberry, QB12, Z, and Personal Computer are strings. Quotation marks are not necessary to define strings.

The expressions outlined in the following table are used to compare strings. The terms s1 and s2 represent string 1 and string 2.

 Expression Operation s1=s2 Returns a true value (–1) if string 1 is identical (including case) to string 2; otherwise, returns a false value (0). Examples: blue=blue is true. blue=BLUE is false. s1!=s2 or s1<>s2 Returns a true value (–1) if string 1 is not identical (including case) to string 2; otherwise, returns a false value (0). Examples: If variable 1 holds the string “thread,” then VARIABLE(1)!=rope is true, and VARIABLE(1)<>thread is false. s1>s2 Returns a true value (–1) if string 1 is greater than* string 2; otherwise, returns a false value (0). Examples: abcd>aabcd is true. a>A is true. s1=s2 Returns a true value (–1) if string 1 is greater than* or equal to string 2; otherwise, returns a false value (0). Examples: a bcd>=abcd is true. a>=A is true. s1<=s2 Returns a true value (–1) if string 1 is less than* or equal to string 2; otherwise, returns a false value (0). Examples: abcd<=abcd is true. A<=a is true. • In a string comparison, the WordPerfect character set values are compared. For information, see “WordPerfect character set values.”

#### Understanding expression evaluation An expression must be written according to the rules and syntax in this appendix for WordPerfect to evaluate it correctly. The following information will help you create and use expressions.

When an expression is encountered in a command, the expression is evaluated first, and the result of the expression is used to complete the command. For example, in the statement ASSIGN(1;VARIABLE(1)+1), the expression is VARIABLE(1)+1.

When the expression is evaluated, the contents of variable 1 are incremented by one. The assignment is then performed, replacing the old contents of variable 1 with the result of the expression.

In several of the expressions, the result of the operation is either true or false. WordPerfect assigns a numeric value to true (–1) and false (0). These values were chosen because they are opposites (numeric complements) of each other. For information, see “Bitwise NOT (!)” in “Understanding expression-related terms.” WordPerfect interprets any non-zero numeric value as true, but when it assigns a value for true, it assigns –1.

In the following example, the first assignment statement assigns false (0) to variable 1, the second assignment statement assigns true (–1), the logical opposite of false, to variable 1, and the third assignment statement assigns true (–1) to variable 1.

`  ASSIGN(1;5=4)   ASSIGN(1;NOT VARIABLE(1))   ASSIGN(1;5!=4)   `

The order in which the various operators are applied in an expression is not simply the order in which they occur. WordPerfect uses an order of precedence that determines which operators are used first, second, and so forth. For example, in the expression 4+7*8, there is a different result depending on whether the addition is performed before or after the multiplication. For more information about the order of evaluation, see “Understanding operator precedence.”

#### Understanding operator precedence WordPerfect supports expressions with several operators. Therefore, an order of evaluation must be followed. The operator precedence is similar to the precedence for mathematical operators.

The following table shows the order in which operators are applied.

Precedence level
Operators
1
– (unary minus)

+ (unary plus)

! (bitwise NOT)

NOT (logical NOT)
2
* (multiply)

/ (divide)

DIV (divide)

% (mod)

MOD (mod)
3
– (subtract)

4
< (less than)

> (greater than)

= (equal to)

!= (not equal to)

<= (less than or equal to)

>= (greater than or equal to)

NOTE: Relational operators also work on strings.
5
& (bitwise AND)

AND (logical AND)

| (bitwise OR)

OR (logical OR)

XOR (exclusive OR)

You can override the operator precedence by placing parentheses around the elements you want evaluated first. Elements inside of parentheses are always evaluated before the elements outside. If parentheses are nested, the innermost parentheses are evaluated first.

In the expression 4+7*8, the multiplication (7*8) is performed first, followed by the addition, because multiplication has a higher precedence than addition. The result is 60. To perform the addition first, type (4+7)*8. In this case, the result is 88.

#### Using merge delimiters A merge delimiter is a word or character that marks the beginning or end of a parameter or argument. Word delimiters can include such reserved words as AND, OR, NOT.

Semi-colons (;) serve as merge delimiters in merge commands with multiple parameters (SUBSTR(expr;start;length). When a semi-colon is encountered during the merge, it is treated as ending the parameter unless the parameter is within quotation marks.

#### Using string delimiters A string delimiter is a character that marks the beginning or end of a string. In string operations, quotation marks (“) serve as string delimiters. In most cases, string delimiters are not needed. Any expression that cannot be evaluated as a valid numeric expression is considered a string.

However, if you want an expression to remain a string even though it could be evaluated as a numeric expression, surround the expression with quotation marks. For example, if you assign a phone number to a variable, but do not surround the number in quotes, the phone number is evaluated as a numeric expression. For example, the command ASSIGN(1;555-1555) assigns the result of subtracting 1555 from 555 (-1000) to variable 1. The command ASSIGN(1;“555-1555”) assigns the string 555-1555 to variable 1.