简体   繁体   中英

TCL split a string

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

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 ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM