Reputation: 151
I am a newbie in TCL and was writing a script where i was trying to figure out a way to split a string using a certain character. For example I have a string like "name1,name2,name3" and then I want to have a for loop which will go through each of the names one at a time and run a command. Is it possible the for loop can split the string and do certain work with it or do i have to split the string (where each "," located) first then have a for loop?
I saw the range option but im never sure on how long the name might be
Upvotes: 2
Views: 5218
Reputation: 246754
Pretty straightforward:
set names "name1,name2,name3"
foreach name [split $names ,] {
puts $name
}
ref:
http://tcl.tk/man/tcl8.6/TclCmd/split.htm
http://tcl.tk/man/tcl8.6/TclCmd/foreach.htm
Particularly note the synopsis of foreach
, where the 2nd arg is a list
foreach varname list body
and the description of split
:
Returns a list ...
Upvotes: 3