Operators

Arithmetic Operators

For arithmetic operators, an operand must be an integer or floating point (real) number.

Operator Definition
+ addition
subtraction
/ division
* multiplication
** power
% modulus

Assignment Operators

Operator Definition
= assignment
+= self addition
-= self subtraction
/= self division
*= self multiplication
%= self modulus

Increment/Decrement Operators

Operator Definition
++ increment
decrement

Logical Operators

Operator Definition
&& logical AND
|| logical OR
! logical negation (NOT)

Bitwise Operators

Bitwise operators are used to compare (binary) numbers.

Operator Name Description
& AND Set each bit to 1 if both bits are 1
| OR Set each bit to 1 if one of two bits is 1
^ XOR Set each bit to 1 if only one of two bits is 1
<< Zero fill left shift Shift left by pushing zeros in from the right and let the leftmost bits fall off
>> Signed right shift Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off.

Relational Operators

The relational operators perform numeric comparisons if both operands are numbers. Otherwise they perform string comparisons (that is, dictionary ordering)

Operator Definition
== equal to
!= not equal to
< less than
> greater than
<= less than or equal to
>= greater than or equal to

String Operators

+ (plus)

The plus indicates the concatenation of two strings.

“ab” + “cd” is equal to “abcd”

~= (tilde equal)

The ~= operator is used in the expression string ~= pattern and returns the following values:

   true if the regular expression pattern is contained in string
   false if the regular expression pattern is not contained in string

If pattern is invalid, MSL returns a run-time error message, and the ~= operation returns false ( pattern not contained).

ip = "192.168.1.1"
if (ip ~= /([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)/) {
    println("IP matches")
}

!~ (exclamation tilde)

The !~ operator is used in the expression string !~ pattern and returns the following values:

   true if the regular expression pattern is not contained in string
   false if the regular expression pattern is contained in string

If pattern is invalid, MSL returns a run-time error message and the !~ operation returns false ( pattern contained).

Ternary Operator

The MSL ternary operator ?: behaves similarly to the C conditional expression in that it connects three operands and offers an alternative way of expressing a simple if..else statement. The format for the MSL conditional expression is as follows:

result = expression ? expression : expression

If the first expression is TRUE (nonzero), then the second expression is evaluated; otherwise expression three is evaluated. The value for the completed conditional expression is the value of the second or third expression, depending on which expression was evaluated. The value of the expression may be assigned to a variable Conditional expressions are most useful in replacing short, simple if..else statements.

For example, the if..else statements

if (x==10) {
    y=10
} else {
    y=20
}

Can be replaced with the one line conditional statements

y = (x==10) ? 10 : 20

These examples perform the same function. If x is 10, then y becomes 10 else y becomes 20.