简体   繁体   English

Tcl字符串操作和替换

[英]Tcl String Manipulation and Substitution

set i 0
set student$i tom

(of course, this is equivalent to set student0 tom ) (当然,这等效于set student0 tom

I want to get the value of student0 , that is, string "tom". 我想获取student0的值,即字符串“ tom”。 How can I get it if I have to use $i to represent 0 here? 如果必须在这里使用$i表示0该如何获取? I tried $(student$i) or $"student$i" and many other ways, but I cannot get string "tom". 我尝试了$(student$i)$"student$i"和许多其他方式,但是无法获取字符串“ tom”。 (I don't want to hard code it like $student0 ) in here. (我不$student0这里那样硬编码$student0 )。

Is there any way to solve this? 有什么办法解决这个问题? Thanks! 谢谢!

You can use the set command -- it returns the value of the variable: 您可以使用set命令-它返回变量的值:

puts "the student is [set student$i]"

However, this sort of thing is generally more trouble than it's worth. 但是,这种事情通常比其价值更大。 Use an array, with i as an index into the array: 使用数组,以i作为数组的索引:

set student($i) tom
puts "the student is $student($i)"

1] set i 0 1]设定i 0

set student$i "tom" 设置学生$ i为“ tom”

puts [subst $[subst student[subst $i]]] 放置[替换$ [替换学生[替换$ i]]]

2] Using Array 2]使用数组

set i 0 设置我0

set student($i) "tom" 设置学生($ i)“ tom”

puts $student($i) 把$ student($ i)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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