Find the value of a variable when variable name is a string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Unicron
    New Member
    • Apr 2007
    • 8

    Find the value of a variable when variable name is a string

    Hey folks. I have searched high and low for the answer to what seems to be a simple problem. Perhaps I don't know how to describe the title properly so if this has been dealt with before, please forgive me.

    I would be glad to take an answer for this in Actionscript, Javascript, or PHP. (because I have run into the problem in all 3 scripting langs.)

    Color1 = "red"
    Color2 = "blue"
    Color3 = "green"

    function showColor(num) {

    .... help here
    }

    The function passes a number only. I want to use showColor(1) to print "red" but can't figure it out.

    The only things I can ever get are undefined variables or the word "Color1".
  • jitu78
    New Member
    • Aug 2006
    • 30

    #2
    Originally posted by Unicron
    Hey folks. I have searched high and low for the answer to what seems to be a simple problem. Perhaps I don't know how to describe the title properly so if this has been dealt with before, please forgive me.

    I would be glad to take an answer for this in Actionscript, Javascript, or PHP. (because I have run into the problem in all 3 scripting langs.)

    Color1 = "red"
    Color2 = "blue"
    Color3 = "green"

    function showColor(num) {

    .... help here
    }

    The function passes a number only. I want to use showColor(1) to print "red" but can't figure it out.

    The only things I can ever get are undefined variables or the word "Color1".
    =============== ==
    Code:
    var col:string;
    
    function showColor(num) {
    if (num==1)
    {col="red";}
    else
    if (mun==2)
    {col="blue";}
    else
    {col="green";}
    trace(col);
    }

    - Jitendra

    Comment

    Working...