Programmer Guide/Concepts/Conditional Expressions: Difference between revisions

From STX Wiki
Jump to navigationJump to search
Line 32: Line 32:
; <code>==</code> : evaluates to true if both sides are equal.
; <code>==</code> : evaluates to true if both sides are equal.
; <code>!=</code> : evaluates to true if sides are not equal
; <code>!=</code> : evaluates to true if sides are not equal
; <code>&lt;/code> : evaluates to true if the left hand side is less than the right hand side
; <code>&lt;</code> : evaluates to true if the left hand side is less than the right hand side
; <code>&gt;</code> : evaluates to true if the left hand side is greater than the right hand side
; <code>&gt;</code> : evaluates to true if the left hand side is greater than the right hand side
; <code>&lt;=</code> : evaluates to true if the left hand side is less than or equal to the right hand side
; <code>&lt;=</code> : evaluates to true if the left hand side is less than or equal to the right hand side

Revision as of 16:44, 14 March 2011

All commands using conditions (IF, DO, WHILE) use the same syntax and evaluation rules. A condition consists of one or more comparisons which are joined with logical (binary) operators. Each comparison compares two values and can evaluate to true or false.

condition = comparison [loperator comparison loperator .. comparison loperator]
comparison
A comparison comprising two expressions separated by a comparison operator. There are two types of comparison supported - see below for comparison operators for details. Both types of comparison can be used within one IF command.
loperator
A logical operator, which combines the results of two comparisons. Logical operators are evaluated strictly from left to right; bracketing is not possible; it is explicitly undefined if a part of an expression that is strictly not required for judging on the truth, or falshood, of the whole expression, gets evaluated, or not.

Logical Operators

The logical operators supported by S_TOOLS-STx conditional commands are the following:

AND
returns true if both comparison results are true
&&
the same as AND
OR
returns true if either of the comparison results are true
||
the same as OR
  • Logical operators are evaluated strictly from left to right.
  • Bracketing is not possible.
  • It is explicitly undefined if a part of an expression that is strictly not required for judging on the truth, or falshood, of the whole expression, gets evaluated, or not. So you should not use expressions with side-effects.

Comparison Operators

S_TOOLS-STx supports two types of comparison operator:

  1. Simple comparison operators for numerical expression and case-insensitive string comparisons.
  2. Pattern matching operators using wild-cards or regular expressions. These pattern matching operators may be applied to strings or, more specifically, to S_TOOLS-STx names.

The general syntax for a comparison is as follows:

expression operator expression
expression
A numerical expression or a string.
operator
A comparison operator (see below).

Simple Comparison

The following operators are supported for simple comparisons:

==
evaluates to true if both sides are equal.
!=
evaluates to true if sides are not equal
<
evaluates to true if the left hand side is less than the right hand side
>
evaluates to true if the left hand side is greater than the right hand side
<=
evaluates to true if the left hand side is less than or equal to the right hand side
>=
evaluates to true if the left hand side is greater than or equal to the right hand side

If the expressions are both numeric, then a numerical comparison is performed. Otherwise, a case-insensitive string comparison is performed.

Pattern Matching

The general syntax of a pattern-matching comparison is:

string operator mask

E.g.:

'the quick brown fox' !SI '*brown*' // search for the word 'brown' in the left hand side string

The string can also be a 'name' - subject to the syntax restrictions for S_TOOLS-STx names. There are two types of pattern matching supported by S_TOOLS-STx conditional commands.

Wild-card pattern matching:

=SI return true if string matches pattern, ignoring the case
!SI return true if string does not match pattern, ignoring the case
=SR return true if the string matches the pattern, respecting the case
!SR return true if the string does not match the pattern, respecting the case
=NI return true if name matches pattern, ignoring the case
!NI return true if name does not match pattern, ignoring the case
=NR return true if the name matches the pattern, respecting the case
!NR return true if the name does not match the pattern, respecting the case

POSIX regular expression pattern matching:

=RSI returns true if the string matches the regular expression, ignoring case
!RSI return true if string does not match the regular expression, ignoring the case
=RSR returns true if the string matches the regular expression, respecting case
!RSR return true if the string does not match the regular expression, respecting the case
=RNI returns true if the name matches the regular expression, ignoring case
!RNI return true if name does not match the regular expression, ignoring the case
=RNR returns true if the string matches the regular expression, respecting case
!RNR return true if the name does not match the regular expression, respecting the case

Regular expressions make heavy use of characters that have a special meaning for S_TOOLS-STx. In order to use such characters, you need to escape them with the S_TOOLS-STx escape character, "`" (back-tick). The S_TOOLS-STx regular expressions are implemented using the TRE library.

Examples

For an example of regular expression in use, see the example script file regular_expressions.sts.

Navigation menu

Personal tools