TCL variable name is a variable value

Posted on 2011-05-11
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


All did not work.
Question by:gs_kanata
    LVL 24

    Expert Comment

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

    LVL 6

    Expert Comment


    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}

    LVL 6

    Expert Comment

    Correction it should be subst not susbst
    LVL 53

    Expert Comment

    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.

    Author Comment

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

    Accepted Solution

    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


    Author Closing Comment

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    This article will show, step by step, how to integrate R code into a R Sweave document
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to dynamically set the form action using jQuery.

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now