Michael_Lowry

Numeric comparison treats null string as negative number

Discussion created by Michael_Lowry on Jun 17, 2016
I just discovered something a bit strange. Sometimes the return code captured by parsing a job log is empty. When this happens, the variable containing the return code will be set to an empty string. I discovered that in AE scripting, a numeric comparison treats the empty string as though it were a number less than zero.
:SET &RETURN_CODE# = ""
:IF &RETURN_CODE# < 0
:  PRINT "Return code is negative."
:ELSE
:  PRINT "Return code is positive."
:ENDIF
This script prints Return code is negative. Is this normal?

I worked around this problem by adding a check for whether the return code contains a numeric value.
:SET &RETURN_CODE# = ""
:IF ISNUMERIC(&RETURN_CODE#) = "Y"
:  IF &RETURN_CODE# < 0
:    PRINT "Return code is negative."
:  ELSE
:    PRINT "Return code is positive."
:  ENDIF
:ELSE
:  PRINT "Return code is non-numeric. Setting it to 1."
:  SET &RETURN_CODE# = 1
:ENDIF
Note: These tests were run in AE v11.2.1.1.

Outcomes