COMPARE function
Syntax
COMPARE (string1, string2 [ , justification ])
Description
Use the COMPARE function to compare two strings and return a numeric value indicating the result.
string1, string2 specify the strings to be compared.
justification is either L for left-justified comparison or R for right-justified comparison. (Any other value causes a run-time warning, and 0 is returned.)
The comparison can be left-justified or right-justified. A right-justified comparison compares numeric substrings within the specified strings as numbers. The numeric strings must occur at the same character position in each string. For example, a right-justified comparison of the strings AB100 and AB99 indicates that AB100 is greater than AB99 since 100 is greater than 99. A right-justified comparison of the strings AC99 and AB100 indicates that AC99 is greater since C is greater than B.
If neither L nor R is specified, the default comparison is left-justified.
The following list shows the values returned:
- -1
- string1 is less than string2.
- 0
- string1 equals string2 or the justification expression is not valid.
- 1
- string1 is greater than string2.
If NLS is enabled, the COMPARE function uses the sorting algorithm and the Collate convention specified in the NLS.LC.COLLATE file in order to compare the strings.
Examples
In the following example, the strings AB99 and AB100 are compared with the right-justified option and the result displayed. In this case the result displayed is -1.
PRINT COMPARE('AB99','AB100','R')
An example in NLS mode follows. It compares the strings anilno and anillo, returning the result as 1. It sets the locale to Spanish and compares the strings again. In this case, the result displayed is -1.
$INCLUDE UNIVERSE.INCLUDE UVNLSLOC.H
x=SETLOCALE( UVLC$ALL, 'OFF' )
PRINT COMPARE( 'anilno', 'anillo', 'L' )
x=SETLOCALE( UVLC$ALL, 'ES-SPANISH' )
PRINT COMPARE( 'anilno', 'anillo', 'L' )
This is the program output:
1
-1