NOTEQUAL relational operator behavior with arrays

Hi, I don’t understand why the NOTEQUAL operator tells that the two segments are the same.
What I’m missing? Thanks in advance (NatOne 9.1.4)

DEFINE DATA 
LOCAL
1 TEST1 (A10)
1 REDEFINE TEST1
2 TEST1_A (A1/10)

1 TEST2 (A10)
1 REDEFINE TEST2
2 TEST2_A (A1/10)
1 I (I1)
END-DEFINE

MOVE 'ABC*' TO TEST1
WRITE TEST1_A(1:4)

MOVE 'ABCD' TO TEST2
WRITE TEST2_A(1:4)

IF TEST1_A(1:4) = TEST2_A(1:4)
   WRITE "Comparing with '=': equals"
ELSE 
   WRITE "Comparing with '=': not equals"
END-IF

IF TEST1_A(1:4) EQ TEST2_A(1:4)
   WRITE "Comparing with 'EQ': equals"
ELSE 
   WRITE "Comparing with 'EQ': not equals"
END-IF

IF TEST1_A(1:4) NOTEQUAL TEST2_A(1:4)
   WRITE "Comparing with 'NOTEQUAL': not same"
ELSE 
   WRITE "Comparing with 'NOTEQUAL': equals"
END-IF

Please see the documentation: (NAT914 is out of support, but this area hasn’t changed) Rules for Arithmetic Assignment, specifically:

  • Condition A:

IF #ARRAY1(1:2) NOT EQUAL #ARRAY2(1:2)

This is equivalent to:

IF (#ARRAY1(1) NOT EQUAL #ARRAY2(1)) AND (#ARRAY1(2) NOT EQUAL #ARRAY2(2))

Condition A is therefore true if the first occurrence of #ARRAY1 does not equal the first occurrence of #ARRAY2 and the second occurrence of #ARRAY1 does not equal the second occurrence of #ARRAY2.

  • Condition B:

IF NOT #ARRAY1(1:2) = #ARRAY2(1:2)

This is equivalent to:

IF NOT (#ARRAY1(1)= #ARRAY2(1) AND #ARRAY1(2) = #ARRAY2(2))

This in turn is equivalent to:

IF (#ARRAY1(1) NOT EQUAL #ARRAY2(1)) OR (#ARRAY1(2) NOT EQUAL #ARRAY2(2))

Condition B is therefore true if either the first occurrence of #ARRAY1 does not equal the first occurrence of #ARRAY2 or the second occurrence of #ARRAY1 does not equal the second occurrence of #ARRAY2.

Thus, you may want to use “IF NOT (TEST1_A(1:4) = TEST2_A(1:4))” as the NOTEQUAL test will only be false if all occurrences of TEST1_A do not match the corresponding elements of TEST2_A. If any occurrences of TEST1_A match then the NOTEQUAL is false. The NOT (TEST1_A() = TEST2_A()) is false if any occurrence of TEST1_A is not equal to corresponding occurrence of TEST2_A.

1 Like

Thank you Douglas, now it’s clear for me.