简体   繁体   中英

Split string in TCL

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM