我想为字符串中的每个字符创建一个foreach循环。
我尝试仅使用foreach
,但是在中断之前只会循环一次。
例如:
set string "a string"
foreach character $string {
puts "a character was counted"
}
我希望上面的代码可以执行puts "a character was counted"
8次(对于“字符串”中的每个字符),但是只能执行一次puts "a character was counted"
答案 0 :(得分:3)
您需要将字符串明确分成字符列表:
foreach char [split $string ""] {...