This article introduces the operators and logic available in DI#, used to compare values, perform calculations, and express conditions in your DI# scripts. You will find the main comparison, arithmetic, and text operators, along with their syntax and behavior. Mastering these operators is essential for writing precise and efficient logical expressions in your Dialog Insight messages.
Comparison Operators
Simples Operators
| Opérateurs simples | ||
|---|---|---|
| Exemple | Type de retour | Description |
| a == b | Equal | TRUE if a equals b. |
| a != b | Different than | TRUE if is not equal to. |
| a > b | Lesser than | TRUE if a is lesser than but not not equal. |
| a<= b | Lesser or equal to | TRUE if a is lesser or equal to b. |
| a > b | Greater than | TRUE if a is greater than b but not equal. |
| a >= b | Greater or equal to | TRUE if a is greater or equal to b. |
Contrary to some languages like PHP, these operators cannot be used on string variables. DI# does not support "alphabetical order" comparisons like "abc"<"bcd" that other languages allow.
Complex Operators
LIKE (and its opposite, NOT LIKE) evaluates a character string based on pattern matching, with the % symbol being used as a wildcard that replaces any number of consecutive characters.
- LIKE "a%" will return TRUE if the string value begins with "a".
- LIKE "%a" will return TRUE if the string value ends with "a".
- LIKE "%a%" will return TRUE if the string contains "a" anywhere in it, including at the beginning or at the end.
- LIKE "th%s" will return TRUE if the string starts with "th" and ends with "s", no matter how many characters are between. "this", "thus", "thanks" would all fit.
- LIKE "abc" with no wildcard (%) has the same effect as the equal operator (==).
CONTAINS (or its opposite NOT CONTAINS): searches inside a string value for the occurrence of a specific character string.
- CONTAINS "abc" will return TRUE if the string value contains "abc" somewhere inside the string, including at the beginning or at the end. It has the same effect as LIKE "%abc%"
- CONTAINS only applies to character strings. It cannot be used to determine whether an array or a list contains a specific value.
IS NULL (and its opposite IS NOT NULL) : tests if a variable contains a value or not. Note that in the case of a string variable, an empty value ("") is NOT the same as a NULL value. A NULL value is when a variable has not been assigned a value at all.
string a = "this is a test"; output.write(a contains "this" ? true : false); // Returns True |
Mathematical Operators
| Opérateurs supportés | ||
|---|---|---|
| Example | Name | Result |
| a + b | Addition | Sum of a and b. |
| a - b | Subtraction | Remainder of b taken away from a. |
| a * b | Multiplication | a multiplied by b. |
| a / b | Division | a divided by b. |
| a % b | Modulo | Take b away from a as many times as possible, the modulo is the remainder (will always be an integer number lesser than b). |
Note: The negation operator is not supported when applied to variables. The -b expression will not compile, try multiplying by -1 instead (b * -1). Because of this, a subtraction written a-b will not compile unless you leave a space after the minus sign (a - b will compile just fine).
Array Operators
Unitary Operators
- Set the value of an item at a specific position of an array:
x[0] = 123; // the value of the 1st item (index 0) is now 123 - Add an item at the end of a dynamic-size array:
x[] += "test"; // add an item "test" at the end of an array of strings - Delete a value from a dynamic-size array:
x[] -= "test"; // all items with the value "test" will be removed
Batch Operators
- Assign multiple values with a single instruction:
x = { "blue", "red", "green" };
// x now contains 3 items - "blue", "red" and "green" - Add multiple items at the end of a dynamic-size array:
x[] += { "blue", "pink", "blue" };
// x now contains 6 items "blue", "red", "green", "orange", "pink" and "yellow" - Delete multiple values from a dynamic-size array:
x[] -= { "blue", "pink"};
// all items with the value "blue" or "pink" will be removed
All the above examples assume that you are using values of the correct data type for the array being modified.