Expressions

Expressions in TrafficScript are created from combinations of literal values, variables, evaluated functions, and operators. Expressions are evaluated when the rule is executed.

You can use expressions to:

Construct complex strings from several different values.

Create complex tests for “if” conditions or “while” loops.

Perform mathematical calculations.

You can use an expression anywhere a literal value (or variable) is suitable. You normally see expressions:

In assignment statements, assigning a value to a variable.

In conditions – “if/else” statements and “while” loops.

As function arguments.

For example:

$message = "The URL path is " . http.GetPath();

$four = 2 + 2;

 

# Sets $ratio to "75%" (for example)

$ratio = ( $a / ($a + $b) * 100 ) . "%";

 

$contentLength = http.getHeader( "Content-Length" );

if( $contentLength > 1024 * 1024 ) {

log.warn( "Large request body: ".$contentLength );

}

Operators

Expressions are constructed from operands (variables, literal values, and so on) and operators. Operators perform calculations or tests on their operands.

Operands in an expression are automatically promoted to the appropriate type: string, integer, or float in accordance with the typecasting rules described in Type Casts in TrafficScript.

The following sections provide details for the operator types you can use.

Mathematical

The operators “+”, “‑”, “*” and “/” treat their operands as integers or doubles and add, subtract, multiply, or divide them.

The prefix operator “‑” promotes its operand to an integer or double and returns its negation.

4 + 7.5 * $a

-$b / $c – 1

The modulus operator “%” takes integer operands, and calculates the remainder after division.

7 % 3 # Returns 1

3 % 7 # Returns 3

String Concatenation

The operator “.” promotes its operands to strings and concatenates them.

"The message is " . $bytes . " bytes long."

( $a / ($a + $b ) * 100 ) . " percent"

The “.=” operator appends its second operand to the first:

$message = "The name is ";

$message .= "Bond, James Bond";

Comparison

The operators “==”, “!=”, “>”, “<“, “>=” and “<=” compare their operands and return true (1) or false (0). If both arguments are strings, a string comparison is performed; otherwise the operands are promoted to integers or floats and compared. For further information on typecasting promotion rules, see Type Casts in TrafficScript.

1 > 3.14 # false

"99" > 100 # false (performs integer comparison)

"99" > "100" # true (performs string comparison)

$a == $b # are the values of $a and $b the same?

Boolean

The operators “&&” and “||” perform boolean “and” and “or” tests. The prefix operator “!” performs a Boolean “not” test.

These operators treat their operands as either true or false, and return true (1) or false (0) accordingly:

A non-zero number operand or non-empty string operand is true.

A zero number operand or empty string operand is false.

"foo" && !0 # true

( 1 < 2 ) && ( 3 < 4 ) # true

$a || $b # true if $a or $b is true

Increment and Decrement

The following table shows the effect of increment and decrement operations:

Operation

Description

$foo++

Increment $foo after it has been referenced

$foo--

Decrement $foo after it has been referenced

++$foo

Increment $foo before it has been referenced

--$foo

Decrement $foo before it has been referenced

$foo += 5

Add 5 to the value of $foo ($foo = $foo + 5)

$foo -= 5

Subtract 5 from the value of $foo ($foo = $foo - 5)

Bitwise Operators

The operators “&” (bitwise-AND), “|” (bitwise-OR), and “^” (bitwise-XOR) perform bitwise operations on their integer arguments. Strings and floats are typecast to integers using TrafficScript typecasting rules (see Type Casts in TrafficScript).

0x1234 & 255 # 0x34

1 | 2 | 4 # 7

1 ^ 3 # 2

The prefix operator “~” (bitwise-NOT) performs a bitwise NOT on its integer argument.

~1 & 0xffff # 65534

The bitwise left-shift and right-shift operators (“<<“ and “>>”) perform left and right bit-shifts on their integer argument.

1 << 2 # 4

2 >> 1 # 1

Assignment Operators

In addition to +=, -=, increment, and decrement operators, TrafficScript supports the following mathematical and bitwise assignment operators:

$foo *= 5             # Product equals ($foo = $foo * 5)

$foo /= 2             # Quotient equals ($foo = $foo / 5)

$foo %= 2             # Modulo equals ($foo = $foo % 5)

$foo <<= 2            # Bit-shift left equals ($foo = $foo << 2)

$foo >>= 2            # Bit-shift right equals ($foo = $foo >> 2)

$foo &= 2             # Bitwise AND equals ($foo = $foo & 2)

$foo |= 2             # Bitwise OR equals ($foo = $foo | 2)

$foo ^= 2             # Bitwise XOR equals ($foo = $foo ^ 2)

Precedence

Complex expressions follow the standard rules of precedence. Parentheses “(“ and “)” can be used to group sub-expressions.

Type Casts in TrafficScript

Variables in TrafficScript can contain data of various types: integer, floating point (double), string, boolean, array, and hash. TrafficScript automatically casts (converts) values and variables into the correct type when you evaluate an expression or call a function. See the following table for details:

Value

Cast to integer

Cast to double

Cast to string

Cast to boolean

Cast to array

Cast to hash

14 (integer)

14

14.0

"14"

true

[ 14 ]

[ ]

3.25 (double)

3

3.25

"3.25"

true

[ 3.25 ]

[ ]

3.75 (double)

4

3.75

"3.75"

true

[ 3.75 ]

[ ]

"abcde"(string)

0

0.0

"abcde"

true

[ "abcde" ]

[ ]

"3.25" (string)

3

3.25

"3.25"

true

[ "3.25" ]

[ ]

"3.75" (string)

4

3.75

"3.75"

true

[ "3.75" ]

[ ]

"14str" (string)

14

14.0

"14str"

true

[ "14str" ]

[ ]

"3.2.7" (string)

3

3.2

"3.2.7"

true

[ "3.2.7" ]

[ ]

0 (integer)

0

0.0

"0"

false

[ 0 ]

[ ]

"0" (string)

0

0.0

"0"

false

[ "0" ]

[ ]

"" (string)

0

0.0

""

false

[ "" ]

[ ]

[ ] (array)

Err

Err

Err

false

[ ]

[ ]

[ "0" ] (array)

Err

Err

Err

true

[ "0" ]

[ ]

[ "A" => 1 ] (hash)

Err

Err

Err

true

[[ "A" => 1 ]]

[ "A" => 1 ]

Strings and doubles are rounded up or down to the nearest integer value when they are cast to integers.

$int = 10;

$double = 2.71828;

 

string.len( $int ); # casts to string, returns 2

string.len( $double ); # casts to string, returns 7

 

# Set $string to "10, 2.71828"

$string = $int . ", " . $double;

 

# Convert $string to a number, and add 4:

$r = $string + 4; # $r is 14