R/List Logical Operator Precedence (Functions/Subroutines/Programs)

Synopsis:  The R/List logical AND operator has a higher precedence than the OR operator.  When in doubt, use parenthesis.

 

 

A problem was reported using reduce where the results were incorrect.  The syntax was something like:

 

… WITH <field-a> EQ <val-1> EQ <val-2> AND WITH <field-b> EQ <val-3> …

 

A simpler statement would process correctly:

 

… WITH <field-a> EQ <val-1> AND WITH <field-b> EQ <val-3> …

 

The reason is operator precedence.  The first is compiled as:

 

<field-a> = <val-1> OR <field-a> EQ <val-2> AND <field-b> EQ <val-3>

 

Which has a precedence of:

 

(<field-a> = <val-1>) OR (<field-a> EQ <val-2> AND <field-b> EQ <val-3>)

 

… since OR is lower than AND in precedence.  This is different from Arev (at least some versions) which would compile it as:

 

(<field-a> = <val-1> OR <field-a> EQ <val-2>) AND (<field-b> EQ <val-3>)

 

Since there was no OR in the simpler statement, it processed as expected.

 

Reducing using the following statement should yield the correct results:

 

… ( WITH <field-a> EQ <val-1> EQ <val-2> ) AND WITH <field-b> EQ <val-3> …

 

As an example, the first works and the second brings back "junk":

 

run rlist "LIST SYSREPOS JUSTLEN 50 WITH TYPEID = 'OIEVENT' AND WITH CLASSID = 'CLICK' BY @ID", 1

 

run rlist "LIST SYSREPOS JUSTLEN 50 WITH TYPEID = 'OIEVENT' = 'OIEVENTEXE' AND WITH CLASSID = 'CLICK' BY @ID", 1

 

And the corrected version of the second:

 

run rlist "LIST SYSREPOS JUSTLEN 50 ( WITH TYPEID = 'OIEVENT' = 'OIEVENTEXE' ) AND WITH CLASSID = 'CLICK' BY @ID", 1

  • kb/kb_articles/kb0147.txt
  • Last modified: 2024/01/30 13:36
  • by 127.0.0.1