Forum Discussion
jsgibbs1
Jul 20, 2015Nimbostratus
foreach command
I need to parse a data file for a custom string and assign the results to a variable. Each string has a similar prefix and is comma-separated.
Example:
custom_string_foo=[get this data w/out th...
Kevin_Stewart
Jul 20, 2015Employee
You can use two options. The first option is to create a simple list object:
set str {test1=[foo],test2=[bar],test3=[blah]}
set mylist [list]
foreach x [split $str ","] {
lappend mylist [lindex [split $x "="] 0]
lappend mylist [findstr $x "\[" 1 "\]"]
}
log local0. "mylist = $mylist"
foreach y $mylist { log local0. $y }
The output of this would be:
mylist = test1 foo test2 bar test3 blah
test1
foo
test2
bar
test3
blah
Where the odd index is the key and the next even index is the value. Or you can use an array:
set str {test1=[foo],test2=[bar],test3=[blah]}
foreach x [split $str ","] {
set thisarray([lindex [split $x "="] 0]) [findstr $x "\[" 1 "\]"]
}
foreach {index value} [array get thisarray] {
log local0. "$index = $value"
}
It's output would be:
test1 = foo
test2 = bar
test3 = blah
And then you could access these associative array indices individually.
log local0. $thisarray(test2)
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