vADC Docs

HowTo: TrafficScript Arrays and Hashes

by aknox on ‎02-24-2013 06:27 AM - edited on ‎06-09-2015 04:22 PM by PaulWallace (2,686 Views)

Array types in TrafficScript

 

Arrays store a collection of values indexed by an integer value - essentially they are a numbered list of TrafficScript variables:

 

$chickens = [ "nugget", "emily", "henrietta", "chelsea" ];

 

Array indexes start at 0, representing the first item in the list, and extend up to the size of the array - so the last index will be the number of items in the array, less one. Arrays can store any value in any index, including other arrays.

 

Arrays can be created by specifying a list of values separated by commas and surrounded by [ ] characters. Alternatively you can create an array of a specific size that is filled with a default value by using the array.create() function:

 

$array = [ 1, 2, 3 ]; # An array of size 3, containing the numbers 1, 2 and 3  
$array = array.create( 10, "foo" ); # An array of size 10, each element is the string "foo"  

 

To look up a value in an array, specify the array variable that contains the value you want to look up, followed by the index of the value enclosed in [ ] characters:

 

$array = [ 1, 2, 3 ];  
$value = $array[0];  # $value will be set to 1 (indexes start at 0)

 

Similarly, to insert a value into an existing array, specify the array and the index into which you want to insert the value, followed by an '=' and the value you want to insert:

 

    $array = [ 1, 2, 3 ];  
    $array[1] = "hello";  # the number 2 will be replaced by the string "hello"

 

You cannot insert into an index that does not exist - if you do, a warning will be printed in the Event Log and the array will remain unchanged:

 

    $array = [ 1, 2, 3 ];  
    $array[3] = "Goodbye";  # This will print a warning and the array will be unchanged 

 

Arrays can be expanded by adding new values to the end using the array.push() function, and they can also be shrunk by removing values from the end with the array.pop() function:

 

$array = [ 1, 2, 3 ];  
array.push( $array, "Goodbye" );  
$value = $array[3];  # $value will be set to "Goodbye"  
  
  
$value2 = array.pop( $array );  # $value2 will also be set to "Goodbye"  
  
$array[3] = "Au revoir";  # This will print a warning because 'pop'  
                          # shrunk the array back to 3 values  

 

Hashes in TrafficScript

 

Hashes are very similar to arrays, however they are indexed by a string value rather than an integer value. Unlike arrays, you do not have to ensure that a particular index already exists before looking it up or before inserting a value into it.

 

Hashes are created by listing pairs of 'keys' (the string index of a value) and their values, separated by =>:

 

    # Create a new hash  
    $hash = [ "name" => "Andy",  
              "occupation" => "Engineer",  
              "height" => 5.11  
            ]; 

 

To look up a value in a hash, you specify the key that indexes the value you want after the hash variable, enclosed in [ ] characters:

 

$height = $hash["height"];  # $height will now be 5.11

 

You can add any key/value pair to the hash at any time, it does not need to exist in the hash before you add it:

 

# You can add a new key/value pair at any time  
$hash["drink"] = "Tea";  

 

Arrays and hashes can contain any TrafficScript data type, including other arrays and hashes. You can index into multiple levels of arrays and hashes by adding additional indexes to the lookup:

 

# You can add the hash to another hash!  
$employees["andy"] = $hash;  
  
# You can then perform multiple lookups to get the values you want  
$occupation_andy = $employees["andy"]["occupation"];

 

To remove a value from the hash you can use the hash.delete() function:

 

hash.delete( $hash, "height" );  # the height/5.11 pair will now  
                                 # be removed from the hash 

 

Enumeration

 

The foreach loop is the easiest way to iterate over an array or hash and perform some actions with each value it contains:

 

foreach( $var in $array ) {  
   ...  # $var will be set to each element of $array in turn  
}

 

For example, the http.listHeaderNames() function returns an array containing the name of each header supplied by an HTTP request. If you want to log all the headers in a request then the foreach loop comes in very handy:

 

    # Log all of the header names and values  
    $headers = http.listHeaderNames();  
    foreach( $header in $headers ) {  
       log.info($header . "=" . http.getHeader($header));  
    }

 

If you want to show the headers in alphabetical order, you can apply the array.sort() function to the array in the foreach loop:

 

foreach( $header in array.sort( $headers ) ) {  
   ...  
}

 

Iterating over the values of a hash can be done in a similar way when using the hash.keys() function. This function returns an array containing all the keys in the hash with which there are associated values. You can then use the foreach loop to iterate over these keys and look them up in the hash.

 

You can therefore use the new http.getHeaders() function as an alternative way to print out all the headers in an HTTP request. The function returns a hash mapping all the header names in the request to their values. To print them all you could write:

 

$headers = http.getHeaders();  
  
foreach( $header in hash.keys( $headers ) ) {  
   log.info( $header . "=" . $headers[$header] );  
} 

 

The output will be the same as that from the previous http.listHeaderNames() example, except that the order in which the headers are printed might differ. The order of the keys returned by hash.keys() is not guaranteed.

 

Read more

 

Contributors