Forum Discussion
Matt_Breedlove_
Nimbostratus
Jun 24, 2014Help with Scan or String first last and multiple/last occurence
Need some help trying to always match the string immediately preceding the first period and delimited by the leading dash
So given these hostnames, the desired match is indicated...I would be wanti...
Kevin_Stewart
Employee
Jun 24, 2014I don't think you necessarily can use scan, because you won't know how many dashed values there are before the period. This is what I might do:
set test [list]
lappend test "prod-tools-discovery.acme.com"
lappend test "prod-tools.acme.com"
lappend test "prod-io-tools.acme.com"
lappend test "stg-io-tools-config.acme.com"
lappend test "stg-io-tools.acme.com"
foreach x $test {
log local0. "input = [lindex [split $x "."] 0]"
set a [lindex [split $x "."] 0]
set b [lindex [split $a "-"] [expr [llength [split $a "-"]] -1]]
log local0. $b
}
I suppose you technically could use scan, but I think it'd be more troublesome:
set test [list]
lappend test "prod-tools-discovery.acme.com"
lappend test "prod-tools.acme.com"
lappend test "prod-io-tools.acme.com"
lappend test "stg-io-tools-config.acme.com"
lappend test "stg-io-tools.acme.com"
foreach x $test {
catch { unset a b c d }
scan [lindex [split $x "."] 0] {%[^-]-%[^-]-%[^-]-%s} a b c d
if { [info exists d] } {
log local0. $d
} elseif { [info exists c] } {
log local0. $c
} elseif { [info exists b] } {
log local0. $b
} else {
log local0. $a
}
}
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)Recent Discussions
Related Content
DevCentral Quicklinks
* 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
Discover DevCentral Connects
