Grouping Operator

Overview of Grouping Operators

Parentheses () are grouping operators. Parentheses can be used to:

  • Alter the precedence of operations so that expressions contained within parentheses are evaluated first,
  • Combine a number of GLASS expressions into a single logical statement, or
  • Clearly show the precedence of operations in complicated expressions to improve code readability.

The generic GLASS syntax for using parentheses is:

(<expression>)

Altering the Precedence of Operations

By default, a GLASS expression joined by the THEN operator has a higher precedence and will be evaluated first before an OR operator.

The order of evaluation of the GLASS expression can be altered with the use of parentheses ().

Take for example the GLASS expressions below:

1 WORD NOCASE 'Folder' OR WORD NOCASE 'File' THEN RANGE DIGIT
2 (WORD NOCASE 'Folder' OR WORD NOCASE 'File') THEN RANGE DIGIT

The GLASS expression in Line 1 matches:

  • Any string containing Folder, or
  • Any string containing File, followed immediately by a single ASCII number (0 to 9).
4 This folder should be encrypted.
5 Folder1 contains sensitive data.
6 Personal details found in File9.

In Line 2, the use of parentheses changes how the expression is parsed by the GLASS engine and now matches:

  • Any string containing either Folder or File, followed immediately by a single ASCII number (0 to 9).
4 This folder should be encrypted.
5 Folder1 contains sensitive data.
6 Personal details found in File9.

Combining Expressions Into a Single Logical Statement

By default, a GLASS operator and/or parameter (e.g. TIMES, REQUIRE, CHECK etc…) is applied only to the GLASS expression immediately preceding it.

Using parentheses (), you can group multiple expressions together so that all expressions within the parentheses are evaluated against a GLASS operator and/or parameter as a single logical statement.

Take for example the GLASS expressions below:

1 RANGE 'A-DH-K' THEN RANGE DIGIT TIMES 6 CHECK 'PASSPORTMOD10'
2 (RANGE 'A-DH-K' THEN RANGE DIGIT TIMES 6) CHECK 'PASSPORTMOD10'

In Line 1, the TIMES parameter and CHECK operator are applied only to the preceding RANGE expression (RANGE DIGIT).

In Line 2, the TIMES parameter is still applied only to the preceding RANGE expression (RANGE DIGIT). However, with the added parentheses, the CHECK operator is evaluated against the whole expression within the parentheses (RANGE 'A-DH-K' THEN RANGE DIGIT TIMES 6).