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 expressionrelated 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 16bit 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.
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 2147 is evaluated as shown in the following table:
The resulting bits represent the number 63, so 2147=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:
where x is the negative number whose equivalent you are trying to find. For example, to find the equivalent of –3,
To find the negative number (n) represented by a given equivalent, use this formula:
where x is the equivalent. For example, to find the negative number represented by 4,294,967,293,
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,
is the same as
For more information about creating numeric expressions, see “Using numeric expressions.”
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 expressionrelated terms.”
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.
•


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 expressionrelated terms.” WordPerfect interprets any nonzero 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.
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.
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.
Semicolons (;) serve as merge delimiters in merge commands with multiple parameters (SUBSTR(expr;start;length). When a semicolon 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;5551555) assigns the result of subtracting 1555 from 555 (1000) to variable 1. The command ASSIGN(1;“5551555”) assigns the string 5551555 to variable 1.