Evaluation of expressions

Expressions with variables can be used as part of the workflow source. Depending on the usage, the expressions are evaluated as arithmetic expressions or using text-based evaluation.

Evaluation as an arithmetic expression is used by:

  • Assignment of a variable (right-hand side of the assignment)
  • Condition in the IF statement (both left and right-hand side)
  • From To expressions of the FOR statement
  • Parameter of the echo: command

Text-based evaluation is used by all workflow commands except echo:

These conditions are applicable for arithmetic Evaluation:

  • If the expression follows a syntax of an arithmetic expression, it is evaluated as a math expression and the result of the evaluation is a number. An arithmetic expression is an expression that only contains basic math operators (+, -, *, /, parentheses), numbers and references to variables.
  • Result of evaluation of the arithmetic expression is a number.
  • If the expression is not an arithmetic expression, it is evaluated as a text-based expression. Text-based evaluation is thus used as a fall-back if an attempt to evaluate the expression using arithmetic fails.

These conditions are applicable for text-based Evaluation:

  • Variables found in the expression are expanded (replaced with their values) and concatenated with the rest of the expression.
  • A reference to a variable that does not exist is not expanded. For example, if variable $def does not exist, then the string $def is left in the resulting text.
  • Result of evaluation of the text-based expression is a text.

Examples of evaluation of expressions

These are the examples of evaluation of expressions:

  • Variable assignment using arithmetics

    Input: Assume two variables set to numeric values

    • $b = 2;
    • $c = 3;

    Workflow contains an expression to be evaluated and assigned to $a:$a = (1 + $b * $c) / 2;

    Result:
    • Variables referenced in the expression are replaced by their values. Because the values of both variables are numeric, and the expression can be evaluated as arithmetic: (1 + 2 * 3) / 2 = 3.5.
    • Variable $a is set to 3.5.
  • Variable assignment, using variables with text and numeric content

    Input: Consider the same expression as in the above example, however, the values of variables used in the expression are different.

    • $b = some text;
    • $c = 3;

    Result:

    Variables referenced in the expression are replaced by their values. Because of the value of b, which is a text, the expression cannot be evaluated as arithmetic. Instead, the expression is evaluated as a text. That is, the variables are replaced with their values and concatenated with the rest of the expression.

    As a result of the evaluation, $a is set to this text:
    (1 + some text * 3) / 2.
  • Evaluation of an expression that is not an arithmetic expression

    Input:

    • $b = world;
    • $a = Hello $b;

    Result:

    Variable in the expression that is assigned to $a is replaced with the value. The result is $a = Hello world;

  • An expression that refers to a variable that does not exist

    Input:

    • $b = 1;
    • $a = 2 * $does_not_exist + $b;

    Result:

    The variable $does_not_exist is not defined. As a result, 'reference to' is not replaced by any value. The variable reference remains in the result. References to variables that are defined ($b) are replaced by their values.

    The result is $a = 2 * $does_not_exist + 1;