Showing results for 
Search instead for 
Did you mean: 

HowTo: TrafficScript variables and types

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


ValueCast to IntegerCast to doubleCast to String
$a = 14; 14 14.0 "14"
$a = 3.25; 3 3.25 "3.25"
$a = 3.75; 4 3.75 "3.75"
$a = "abcde"; 0 0.0 "abcde"
$a = "3.25"; 3 3.25 "3.25"
$a = "3.75"; 4 3.75 "3.75"
$a = "14str"; 14 14.0 "14str"
$a = "3.2.7"; 3 3.2 "3.2.7"


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  


In a mixed-type operation such as a compare, where all types are allowed:


  1. If either operand is a double, both operands will be converted to doubles, else:
  2. If either operand is an integer, both operand will be converted to integers, else:
  3. Both operands are strings, in which case a string operation is used.


Arrays and hashes


The syntax and use of arrays and hash types is reviewed in the article HowTo: TrafficScript Arrays and Hashes.


Casting to or from arrays and hashes is not permitted. For example, if an array is used where a string value is required, a warning message will be printed to the Event Log. The warning will contain the line number at which the problem occurred so it can be easily located and corrected. A conversion of this type will have a result of 0 and the rule will continue to execute.


Adding the clause import strict; to the rule will cause the rule to be terminated should any type casting errors occur.


$array = [ 1, 2, 3 ];  
$header = http.getHeader( $array );  
http.setHeader( "X-Test", "My Value" ); 


...will print the following log line, but will still add the "X-Test" header to the request and send it to the server:


WARNING: Rule <Rule Name>: Line 3: Cannot convert array to string value


The following rule attempts to authorize users by checking the value of the 'X-Username' header. There is a bug in the rule though - the author forgot to look up the username in the $authorized hash. If this rule was controlling access to a secure part of the website then it could be a security risk to continue processing the rule with an error. To protect against such an occurrence the import strict; clause is used:


import strict;  
$authorized = [  
  "andy" => 1,  
  "matt" => 0  
$username = http.getHeader( "X-Username" );  
if( $authorized != 1 ) {  
   http.sendResponse( "401 Unauthorized", "text/plain", "", "" );  


When this rule is executed, it will reach the line containing the error and then abort because it is using strict error checking. The connection to the client will be closed and the client will see a 500 error message. The following log messages will also be printed:


WARNING: Rule aborted during execution - Strict error checking enabled

WARNING: Rule <Rule Name>: Line 8: Cannot convert hash to integer value


Read more

Version history
Revision #:
1 of 1
Last update:
‎02-24-2013 06:30:AM
Updated by:
Labels (1)