You can use if and case commands for implementing conditions and branching in transformation scripts.

If command

The if command is the most basic of all the control flow statements. It tells script to execute a certain section of code only if a particular test (condition) evaluates to true. Condition can be any expression containing boolean, integer and strings logic. Integer expression is evaluated to true when greather than 0 and string expression is evaluated to true when returns non blank value. It is recommended to use boolean expressions to maintain script readability.
Example of basic if command usege:

val = value() as double
result = "Success"
if (val > 100) result = "Too high"
return result

The result value is set to "Too high" only if source value of the row is greater than 100. Otherwise the result in row after transformation is "Success".
You can use multiple commands after if command when you use brackets:

val = value() as double
result = "Success"
if (val > 100) {
   result = "Too high"
   result += " (" + val.toString() + ")"
}
return result;

If comman can contain an else part to provide a secondary path of execution when an "if" clause evaluates to false. We can rewrite our first example as:

val = value() as double
if (val > 100) {
    result = "Too high"
} else {
    result = "Success"
}
return result;

In case of simple conditions you can also use a "?, :" if notation for branching single commands:

val = value() as double;
return val > 100 ? "Too high" : "Success";

Switch command

Unlike if and if else commands, the switch statement allows for any number of possible execution paths. You can use more sophisticated conditions. Following example ilustrates how to evaluate value x three different ways:

  1. Equals a specified string value
  2. Is one of the values from list
  3. Is number whithin an range
switch (x) {
    case "Specific string value":
         result = "Contains specified string value"
         break
    case [4, 5, 'a', 'b']:
         result = "Is 4, 5, a, or b."
         break
    case 12..30:
         result = "In range"
         break
    case Number:
         result = "Is number"
         break
    default:
         result = "Default"
}

Another point of interest is the break statement after each case. Each break statement terminates the enclosing switch statement. Control flow continues with the first statement following the switch block. The break statements are necessary because without them, case statements fall through; that is, without an explicit break, control will flow sequentially through subsequent case statements.

Technically, the final break is not required because flow would fall out of the switch statement anyway. However, we recommend using a break so that modifying the code is easier and less error-prone. The default section handles all values that aren't explicitly handled by one of the case sections.

Deciding whether to use if command or a switch statement is sometimes a judgment call. You can decide which one to use based on readability and other factors. If you have more than 2 ways branching, use switch command.
You can naturally nest the if and switch commands.

Samples

Return text according to value
hodnota = value() as double;
result = "Lower than 100";
if (hodnota > 100) result = "Greater than 100";
return result;
Returns a negative or positive value of a column 5 according to text value in column 6
if (value(6) == 'Credit') { return value(5)
} else if (value(6) == 'Debit') { return -value(5)
} else { return 0
}

For details about accessing other columns in script, see [Transformations over multiple columns].

  • No labels