Passing a TCL list to iRules LX

Problem this snippet solves:

This is a very simple code share for accessing list elements passed to iRules LX. When passing arguments to iRules LX sometimes you don't beforehand how many are being sent, so passing a list of n arguments is a logical solution.

The issue is how to access them. Typically arguments are accessed using the code

req.params()[0]
for the first argument and
req.params()[1]
for the second etc.

When you pass a list, it ends up getting sent as a nested array, so using

req.params()[0]
will not work, it will retrieve all of the list elements as one. For example, a tcl list (
set mylist {red green blue yellow
) would return [ 'red', 'green', 'blue', 'yellow' ]

req.params()[1]
would return 'undefined'

Fortunately the fix is an easy one, you just need to reference the nested element

req.params()[0][0]
and
req.params()[0][1]
etc.

Using a simple for loop, you can iterate through all the list elements.

How to use this snippet:

Just add an extra array element when referencing the corresponding list element passed from TCL. Simples

Code :

// simple for loop to iterate through list elements passed from TCL    
for (var i = 0; i < req.params()[0].length; i++) {  
    var foo = req.params()[0][i]
    console.log('List element: ', foo);
}

Tested this on version:

12.1
Updated Jun 06, 2023
Version 2.0
No CommentsBe the first to comment