Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

create variable name using a variable?

Status
Not open for further replies.

theotrain

Programmer
Mar 5, 2003
150
MX
i want to create variables in a loop, so that instaed of saying:

Code:
var1= "something"

I want the "1" in "var1" to be a variable.

In flash it would be something like:
Code:
_root["var"+variable]= "something"

...I'm just looking for the director equivelant. thanks!
 
Say you want to create global variables called "var1", "var2", ... "var6" and assign values "something1", "something2" ... "something6" to them dynamically using a loop.
Code:
--
on startMovie
  repeat with i = 1 to 6
    tempVarName = "var" & i
    (the globals)[symbol(tempVarName)] = "something" & i
  end repeat
end startMovie
--
That's it. Check the value in the Message window:
Code:
--
put var4
-- "something4"
--
 
thats perfect, thanks.

just out of curiosity, is there a way to do this if you dont want to use globals? it seems here were using
Code:
the globals
which is a list, is there such thing as a list of local variables, or is there a way to code this without using a list?

just wondering...
 
So, you want to create variables with limited scope. They are called
Code:
property
. To create
Code:
property
dynamically, use Property List:
Code:
--
localPropList = [:]
repeat with i = 1 to 6
  localPropList[symbol("var" & i)] = "propValue" & i
end repeat
repeat with j = 1 to 6
  put "var" & j & ":" && localPropList["var" & j]
end repeat
--
This will return:
Code:
--
-- "var1: propValue1"
-- "var2: propValue2"
-- "var3: propValue3"
-- "var4: propValue4"
-- "var5: propValue5"
-- "var6: propValue6"
--
Also, you can create Property Variables in a Child Object dynamically using
Code:
setaProp
:
Code:
--
childObject = script("parent script").new()
setaProp(childObject, #newProperty, "newValue")
put childObject.newProperty
--
This returns:
Code:
--
-- "newValue"
--
 
brilliant. it seems strange that they would confine the ability to use dynamic variable names to lists, even though thats probably where you would need them most. Maybe Im just accustomed to sloppy programming but I've often had a bunch of variables like
Code:
var1,var2,var3...
without putting them into a list. I guess it just seemed like one less thing to remember.

thanks, you've been a great help.
 
Hi I hvea simil ar problem but instead of putting
the value in global variables such as var1, var2 etc I want to retrieve globla variables var1,var2. Please help
 
Hi,
this is something along the line of my problem. I'm wanting to create a 2 dimensional array. such as ...

global array(10,10)
array(1,1) = 1

can this be done without lists ?
 
In Director, array is called
Code:
list
, you cannot create array without using
Code:
list
!!!

To create a
Code:
list
:
Code:
--
array = list("var1", "var2", "var3")
--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top