Forum Discussion
Help with Scan or String first last and multiple/last occurence
This is a derivative of what Patrik suggested that I came up with as a result of also in some cases needing to match the second to last field of dash delimiters in a hostname
I think this is the best way to do this with lindex because it removed the dependency on the number of periods in the original hostname. Now the only dependency is that the target part of the hostname containing dashes is on the leftmost side of the hostname
So to find the last field of a dash delimited string in an HTTP::host
set httphost "test-tools-config.acme.com"
set a [string tolower [lindex [split [lindex [split [getfield $httphost ":" 1] "."] 0] "-"] end]]
a = config
And to find the second (or N) to last field of a dash delimited string in HTTP::host
set httphost "test-tools-config.acme.com"
set b [string tolower [lindex [split [lindex [split [getfield $httphost ":" 1] "."] 0] "-"] end-1]]
b = tools
The only reason that lindex can do this with variable field delimiters where scan, string, etc fail, is that it allows the "end" and "end-N" operator
Honestly, I think a built in function is needed in irules similar to "getfield", but would be "reverse" to reverse a string. I believe native TCL has a "string reverse", but it is not enabled for some reason in irules : (. Perhaps it doesnt gain mutch anywa since with lindex I must do two splits and two lindex's and one getfield to accomplish this. With a reverse function it would be just less lindex's and more reverse's The goal was really to use SCAN, but unless irules is going to allow a custom flag in SCAN similar to "end" and "end-N" then I guess it just all boils down to string/array functions
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)Recent Discussions
Related Content
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com
