Forum Discussion
reverse a string or list
The iRules TCL implementation does not have a string reverse or list lreverse function. Any ideas on how I could accomplish that? I'm capturing a DNS PTR query and reversing the result of the DNS::question name.
Thanks, Mitch
3 Replies
- Brad_Parker
Cirrus
you could try something like this:
set name [DNS::question name] set reverse "[getfield $name "." 4].[getfield $name "." 3].[getfield $name "." 2].[getfield $name "." 1]" - Michael_Jenkins
Cirrostratus
A method I've used to reverse order a delimited string would be like this (tested in
)tclshset strOrig "aaa.bbb.ccc.ddd" set listOrig [split $strOrig "."] set listReversed [list ] for {set i [llength $listOrig]} {[incr i -1] >= 0} {} {lappend listReversed [lindex $listOrig $i]} set strReversed [join $listReversed "."] - StephanManthey
Nacreous
or a slightly more generic approach in case you don´t have a fixed number of digits and want to use list functions:
set ip_addr [IP::client_addr] set separator "." for { set digit [expr {[llength [split $ip_addr $separator]] -1 }] } { $digit >= 0 } { incr digit -1 } { lappend reverse_order [lindex [split $ip_addr $separator] $digit] } set reverse_ip [join $reverse_order $separator]But for sure Brad´s solution is way faster. +1 🙂
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