Relational Expressions

A relational expression consists of a relational operator applied to a pair of expressions. Three types of relations are discussed in this section:

Relational Operators

Relational Operators used in DataBasic are shown in the following table.

Symbol Operation
< or LT less than
> or GT greater than
<=, LE, =< or #> less than or equal to
>=, GE, => or #< greater than or equal to
= or EQ equal to
#, <>, ><, or NE not equal to
MATCH or MATCHES pattern matching

A relational operation evaluates to 'one' if the relation is true and 'zero' if the relation is false.

Relational operators have lower precedence and are, therefore, evaluated after all arithmetic and string operations have been performed, unless they are placed in parentheses in which case the precedence is raised.

Go to top buttonArithmetic Relations

An arithmetic relation is a pair of arithmetic expressions separated by any one of the relational operators. For example:

Z = X > 4  If X greater than 4, Z is set to one (true), but if X less than or equal 4, Z is set to zero (false).

A = B < 4*2  If B is less than 4 times 2 (8), A is set to one (true), but if B is greater than or equal to 8, A is set to zero (false).

P = Q NE 4+6  If Q is not equal to 10 (4 plus 6), P is set to one (true), but if Q is equal to 10, P is set to zero (false).

String Relations

A string relation comprises a pair of string expressions, or a string expression and an arithmetic expression, separated by any one of the relational operators.

Note: Two arithmetic expressions are always evaluated as an arithmetic relation.

Comparing Two Strings of the Same Length

In string relations, the characters in the two strings are compared one at a time from left to right. Each successive pair of characters is evaluated by the specified relational operator according to their numeric ASCII code values.

The first pair of ASCII character codes values to meet the relational condition, specified by the operator, causes the string relation to evaluate to 1 (true). If none of the corresponding character comparisons meet the specified condition, the string relation evaluates to 0 (false). For example:

"AAB" > "AAA"

evaluates to 1 (true), because the ASCII value of the third character B (66) is greater than the ASCII value of A (65).

while:

"AAB" > "ABC"

evaluates to 0 (false), as the ASCII value of the second character in the first string (A, ASCII value 65) is less than that of the second character in the second string (B, ASCII value 66).

Comparing Two Strings of Different Length

If two strings are not the same length, but the shorter string is identical to the beginning of the longer string, the longer string is considered to be greater than the shorter string. The right-most overlapping' character of the longer string is in fact compared with ASCII null producing a "greater than" condition.

Note: The null string, in general, is treated as less than zero.

For example:

"STRINGS" GT "STRING"

This relation evaluates to 1 (true) with the ASCII equivalent of S being 83 and the ASCII equivalent of the null string zero.

Comparing a String with an Arithmetic Expression

If a string is compared to an arithmetic expression, the result of the arithmetic expression is treated as a string expression. For example:

24 * 6 GT "14%"

evaluates to 1 (true). The arithmetic expression evaluates to "144" which is compared with "14%". The ASCII value of 4 (51) is greater than % (37), hence the expression is 'true'.

Further examples of string relational expressions are provided below:

"AND" EQ "BUT"

evaluates to 0 (false), because the ASCII value of A (65) does not equal the ASCII value of B (66).

"BILL" < 5431

evaluates to 0, because the ASCII value of B (66) is greater than the ASCII value of 5 (53).

12*4 > "AB"

evaluates to 0 (false), because the ASCII values of 4 (52) is less than the ASCII value of A (65). Remember. (The result 12 times 4 equals 48 is evaluated first).

12*9 # "108"

evaluates to 0 (false), because the result of 12 times 9 is 108 which is equal to the string "108"

0 > ""

evaluates to 1 (true). The ASCII value of decimal zero is 48 which is greater than the ASCII value of null (0).

Go to top buttonPattern Matching

The MATCH (or MATCHES) relational operator is used to compare a string expression to a predefined pattern or patterns.

Each pattern is defined as a specified, or unspecified, number of numeric, alpha or alphanumeric characters, a specified literal string, or a combination of these, and must account for all characters in the expression for the relation to be true.

More than one pattern can be defined for matching to the expression. Multiple patterns must be separated by value marks (X'FD' or @VM).

If the expression matches at least one of the patterns, the string relation evaluates to 1 (true). If it does not match, it evaluates to 0 (false).

Syntax

expression MATCH{ES} "pattern{]pattern}..."

Syntax Elements

expression is the string expression to be matched.

] is a value mark. (X'FD' or @VM, equivalent to CTRL+])

pattern is a string value enclosed in quote marks that specifies the pattern of characters to be found. It is specified using any number and combination of the following, where n is an integer:

Note: Quotes are required to enclose the pattern(s). If a literal string is included, the pattern should be enclosed in double quotes, with single quotes around the literal string, or vice versa.

If the integer literal (n) used in pattern is 0, the relation evaluates to true if zero or more characters in the string or substring match the specification letter. A null string matches 0A, 0N, 0X and "".

Examples

X = Z MATCHES '9N'

This example evaluates to 1 (true) if the current value of Z consists of 9 numeric characters, otherwise it evaluates to 0.

X = B MATCH '3A"-"2N"-"4X'

evaluates to 1 if B consists of three alphabetic characters, followed by a hyphen, two numerics, a hyphen, and four alphanumerics, otherwise it evaluates to 0.

X = A MATCHES "0N'.'0N"

evaluates to 1 if the current value of A is any number containing a decimal point, or just a decimal point by itself; otherwise, it evaluates to 0.

X = V MATCHES "'£'1N','3N','3N"

evaluates to 1 if V contains a string containing a pound sign followed by one numeric, three numerics and three numerics again with commas separating them (for example, "£1,456,567"); otherwise it evaluates to 0.

X = Y MATCH ''

evaluates to 1 if Y contains a null string; otherwise it evaluates to 0.

X = A MATCHES "1A4N"

evaluates to 1 if the string value of A consists of one alphabetic character followed by four numerics, otherwise it evaluates to 0.

X = Y MATCHES "1N3A]1N3A2N"

evaluates to 1 if the string value of Y consists of one numeric followed by three alphabetic characters or one numeric followed by three alphabetic characters, followed by two more numerics, otherwise it evaluates to 0.

Go to top button