Split string in TCL

2k views Asked by At

I'm coding an iRule on our F5 server but I'm a complete beginner when it comes to TCL.

I have a HTTP::host variable that contain a hostname in the following format: application-dev.com

All I'm trying to do is split this string where the hyphen occurs and set the first and second sections to separate variables. So I would end up with this:

variable1 = application
variable2 = dev.com

I've gotten this far:

set hostSections [split [HTTP::host] "-"]

But can't find any information on how to assign the sections to seperate variables

1

There are 1 answers

2
Jerry On

You can use lindex (list index) for older versions of Tcl:

set variable1 [lindex $hostSections 0]
set variable2 [lindex $hostSections 1]

Since lists are 0-indexed, 0 will indicate the first element of the list.

In Tcl 8.5 and later, you can use lassign which makes things shorter:

lassign [split [HTTP::host] "-"] variable1 variable2

Both ways stores the values in the variable names variable1 and variable2.