Can actionscript do variable in variable?

I know in php etc its great with variables. You can use variables withinn variables to call another variable

e.g $somevar1, $somevar2

then $i = 1.

$$somevar$i (this will be treated as $somevar1)

Is it possible to do this in actionscript, because it saves one hell of alot of code.

Sure you can. It’s called associative array referencing.


myvariable = "a nice variable";
firstpart = "my";
trace(this[firstpart+"variable"])

The expression in the [] will be evaluated first, and then used as name for the property/method/… to retrieve.

Awesome thanks!

Actually some really weird stuff is happening now, i wonder if you can tell me why.

I have a variable content0, content1 etc. Which have data loaded in the first frame.

When i click this button it loads the corresponding content.

if i do.

_root.mainText.scrollBox.htmlText = content0;

That works fine, yet if i do

 
action = "0"; 
_root.mainText.scrollBox.htmlText = this["content" + action];

it comes up undefined! Unless i put a content0 = content0;
before it.

Why would this happen it seems very silly to me!

Please note that content0 is a global variable from another frame, so maybe this has something to do with it?

bleh… its funny how when you write a post it makes you think about it.

of course i needed to do

_global[“content” + action]

NOT

this[“content” + action]

because its a global variable.

Yep :slight_smile: