Alphanumeric comparisons
An alphanumeric comparison is a comparison of the single-byte character values of two operands.
When one of the operands is neither class alphanumeric nor class alphabetic, that operand is processed as follows:
- A display floating-point data item is treated as though it were a data item of category alphanumeric, rather than as a numeric value.
- A zoned decimal integer operand is
treated as though it were moved to a temporary elementary data item
of category alphanumeric with a length the same as
the total number of digits in the number, according to the rules
of the MOVE statement.
When the ZWB compiler option is in effect, the unsigned value of the integer operand is moved to the temporary data item. When the NOZWB compiler option is specified, the signed value is moved to the temporary data item. See ZWB in the COBOL for Linux® on x86 Programming Guide for more details about the ZWB (NOZWB) compiler option.
Comparison then proceeds with the temporary data item of category alphanumeric.
Comparison of two alphanumeric operands
The collating sequence used for comparison is determined by the PROGRAM COLLATING SEQUENCE clause in the OBJECT-COMPUTER paragraph and the setting of the COLLSEQ compiler option.
If the PROGRAM COLLATING SEQUENCE clause is specified with an alphabet-name of STANDARD-1, STANDARD-2, or EBCDIC, the COLLSEQ compiler option is ignored. The collating sequence is the one associated with the specified alphabet-name. The comparison proceeds as described below for Standard comparison.
If the PROGRAM COLLATING SEQUENCE clause is not specified or is specified with an alphabet-name of NATIVE, the method of comparison is determined by the COLLSEQ compiler option:
- If the COLLSEQ(BINARY) compiler option is in effect, the collating sequence is determined by the binary values of characters. The comparison proceeds as described below for Standard comparison.
- If the COLLSEQ(EBCDIC) compiler option is in effect, the EBCDIC collating sequence is used. The comparison proceeds as described below for Standard comparison.
- If the COLLSEQ(LOCALE) compiler option is in effect, the collating sequence is determined by the runtime locale. For purposes of comparison, trailing spaces are truncated from the operands except that an operand consisting of all spaces is truncated to a single space. Locale-based comparison is not necessarily a character-by-character comparison. If the shorter operand were extended with spaces as for a nonlocale-based comparison, the result might not be the culturally-expected result. For information about locales, see Locale considerations.
If the PROGRAM COLLATING SEQUENCE clause is specified with an alphabet-name that references a collating sequence defined by literals, the collating sequence is determined by the order of the specified literals and the sequence indicated by the COLLSEQ compiler option, as described for the ALPHABET clause. The comparison proceeds as described below for Standard comparison.
- Standard comparison
A standard comparison is any comparison that is not based on a locale. The standard comparison method depends on whether the operands to be compared are of equal length or unequal length.
If the operands are of unequal length, the comparison proceeds as though the shorter operand were padded on the right with appropriate space characters to make the operands of equal length. The comparison then proceeds according to the rules for the comparison of operands of equal length.
If the operands are of equal length, the comparison proceeds by comparing corresponding character positions in the two operands, starting from the leftmost position, until either unequal characters are encountered or the rightmost character position is reached, whichever comes first. The operands are determined to be equal if all corresponding characters are equal.
The first-encountered unequal character in the operands is compared to determine the relation of the operands. The operand that contains the character with the higher collating value is the greater operand.