all groups > flash actionscript > january 2005 >
You're in the

flash actionscript

group:

referencing a variable from a string + variable


referencing a variable from a string + variable Mike7222
1/28/2005 11:12:08 PM
flash actionscript: Hope I can explain this clearly.

I have an array with 3 elements. I also have 3 variables , one for each of
the elements.
I am seacrhing the array for the index number of one of the variables, in this
case it is "blue"
I then want to update the poscorrect variable that is associated with the
value "blue" by writng the "poscorrect" as a string and then adding the index
number on the end to reference the right variable ie - "poscorrect" +i (which
I want to return poscorrect1 in this case). I know that I am using incorrect
sytax with my attempt (as you can see below i tried using eval, but it returned
errors) but does anyone know how I do it?

Thanks alot,
Mike



//declare the vars
poscorrect1 = false;
poscorrect2 = false;
poscorrect3 = false;


//declare the array and elements
var colors = ["red", "blue", "green"];
trace (colors);

//search the array for blue, it returns value 1, i = 1 after this executes
for (var i = 0; i <= 2; ++i) {
trace ("Now searching element" + i);
if (colors == "blue") {
trace ("the location of blue is " +i);
eval ("poscorrect"+i) = true;
break;
}
}

trace (poscorrect1);
trace (poscorrect2);
trace (poscorrect3);
Re: referencing a variable from a string + variable kglad
1/29/2005 3:27:40 AM
if (colors[ i ] == "blue") {
trace("the location of blue is "+i);
this["poscorrect"+i] = true;
break;
AddThis Social Bookmark Button