这里我想使用与上面相同的开关。$list包含a,b &c.我想打印苹果球相应的猫。我想多次使用/引用这个switch语句
switch $blk {
a {puts "Apple"}
b {puts "Ball"}
c {puts "Cat"}
default {puts "Nothing"}
}
foreach item $list {
// Here I want to use the exact same switch as I used above. $list contains a,b & c. I want to print Apple Ball & Cat accordingly. I want to use/reference this switch statement multiple times
}
一种方法是把你想重用的代码放在一个进程中,例如
proc myswitch item {
switch $item {
a {puts "Apple"}
b {puts "Ball"}
c {puts "Cat"}
default {puts "Nothing"}
}
}
,然后像这样使用
myswitch $blk
foreach item $list {
myswitch $item
}
另一种方法是共享switch
命令的script参数:
set body {
a {puts "Apple"}
b {puts "Ball"}
c {puts "Cat"}
default {puts "Nothing"}
}
switch -- $blk $body
foreach item $list {
switch -- $item $body
}