[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

TCL variable name is a variable value

Posted on 2011-05-11
9
Medium Priority
?
703 Views
Last Modified: 2013-11-22
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
Comment
Question by:gs_kanata
7 Comments
 
LVL 24

Expert Comment

by:fridom
ID: 35744009
You usually would not do it that way but making Bus_Route an Array, the index then would be you $Number.

0
 
LVL 6

Expert Comment

by:bummerlord
ID: 35757125

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
 
LVL 6

Expert Comment

by:bummerlord
ID: 35757186
Correction it should be subst not susbst
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 53

Expert Comment

by:Dhaest
ID: 35953341
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
 

Author Comment

by:gs_kanata
ID: 35953343
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
 
LVL 6

Accepted Solution

by:
bummerlord earned 1500 total points
ID: 35985653
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
 

Author Closing Comment

by:gs_kanata
ID: 35997162
B/c I have to design those variables in advance. It is not so dynamic if there is a wide range of possibilities.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Make the most of your online learning experience.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question