REXX coding hints and tips

Use strict comparison for strings, rather than using REXX's looser comparison.

REXX draws a very important distinction between using = for an equality test and using ==. (Similar comments also apply to < and <<, and other operators, but the most important issue is equality testing.)

When you use ==, REXX treats the arguments as strings and compares the arguments unchanged. However, when you use =, REXX attempts to transform the arguments prior making the comparison. REXX first tries to convert both arguments to numeric values; if this fails, it treats the arguments as strings, stripping leading and trailing blanks and then, if the resulting strings are of unequal length, padding the shorter string on the right with blanks.

For File Manager REXX programming, a good practise is to use == for all string comparisons, and to use = only with care for numeric comparisons.

When you use the strict equality operator (==), you must pad literals with blanks as required. For example, if the first 10 bytes of each record contain an uppercase name with trailing blanks, the result of the following statement is true:
FLD(1,10) = 'FRED'
but the following statement is false:
FLD(1,10) == 'FRED'
To avoid the problem shown by the second example, (but still keeping with the preferred use of == rather than the looser =), you could code either of the following alternatives:
  • STRIP(FLD(1,10)) == 'FRED'
  • FLD(1,10) == 'FRED ' (six trailing blanks)

Ultimately, the last choice is best because it allows FASTREXX processing, which the use of the STRIP function does not.

Here are some examples of possibly unexpected results:
REXX Result
IF 'A ' = ' A' True
IF 'C140'x = '40C1'x True
IF 'A ' == ' A' False
IF '01' = '1 ' True
IF '001' = '1E0' True (1E0 is scientific notation)
IF '+ 1.00' = ' 1E0' True
IF '12345678901' = 12345678902 True (it's longer than the default REXX numeric precision for integers)

Error handling

To turn off File Manager REXX error handling, add the following REXX statement:
SIGNAL ERROR OFF

This is because File Manager always performs a SIGNAL ON ERROR NAME label which traps non-zero return codes.