Tcl字符串操作和替换

use*_*478 1 string tcl substitution

set i 0
set student$i tom
Run Code Online (Sandbox Code Playgroud)

(当然,这相当于set student0 tom)

我想得到的值student0,即字符串"tom".如果我必须在这里$i代表,我怎么能得到它0?我试过$(student$i)$"student$i"许多其他方式,但我不能得到字符串"汤姆".(我不想硬编码$student0)在这里.

有什么方法可以解决这个问题吗?谢谢!

Bry*_*ley 6

您可以使用该set命令 - 它返回变量的值:

puts "the student is [set student$i]"
Run Code Online (Sandbox Code Playgroud)

然而,这种事情通常比它的价值更麻烦.使用数组i作为数组的索引:

set student($i) tom
puts "the student is $student($i)"
Run Code Online (Sandbox Code Playgroud)

  • @potrzebie它可能更高效,但当你想把一些代码放在一起时,数组非常方便...... (2认同)