• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 717
  • Last Modified:

TCL variable name is a variable value

I have a TCL variable. But part of its name is the value of anotehr dynamic variable. For instance, the variable "Bus_Route_$Number" represent Bus route number. But the Number itself is a variable, could be 1,2,3 and etc. It is either from environment variable or runtime execution result of another part of program. Now if I want to retrieve the value of  Bus_Route_$Number, how to specify it. I tried

$Bus_Route_$Number
${Bus_Route_$Number}
${Bus_Route_${Number}}

All did not work.
0
gs_kanata
Asked:
gs_kanata
1 Solution
 
fridomCEO/ProgrammerCommented:
You usually would not do it that way but making Bus_Route an Array, the index then would be you $Number.

0
 
bummerlordCommented:

Agree with fridom, that is usually not the way to go about since it's not very efficient, but it can be done, of course :-)

This is one way to do it;

set var "\$Bus_Route_$Number"
set value [susbst -nocommands $var]

Lets say $Number contained 42, then $value now has the same data as in ${Bus_Route_42}

0
 
bummerlordCommented:
Correction it should be subst not susbst
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
DhaestCommented:
I've requested that this question be closed as follows:

Accepted answer: 500 points for bummerlord's comment http:/Q_27031495.html#35757125

for the following reason:

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
 
gs_kanataAuthor Commented:
set Number 42
set var "\$Bus_Route_$Number"
set value [subst -nocommands $var]

But I got error when running the above lines

can't read "Bus_Route_42": no such variable
    while executing
"subst -nocommands $var"
0
 
bummerlordCommented:
That is because you have not yet defined "Bus_Route_42"
42 was just an example.

Complete PoC code attached - it will output;
Variable Bus_Route_42 contains "sample data"


# Define sample environment data
set Bus_Route_42 "sample data" 
set Number 42

# Proof of concept code
set var "\$Bus_Route_$Number"
set value [subst -nocommands $var]
puts stdout "Variable Bus_Route_$Number contains \"$value\""

Open in new window

0
 
gs_kanataAuthor Commented:
B/c I have to design those variables in advance. It is not so dynamic if there is a wide range of possibilities.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now