Skip navigation
Currently Being Moderated

Include variables with minor difference in a loop

Mar 20, 2012 2:09 PM

How can I include variables with minor change inside a loop instead of hard code each one?

 

For example

 

aList = ""A", "B", "C", "D"]

repeat with i = 1 to 4

  temp = aList[i]

  Variable[temp] = 1 (some calculation)

end repeat

The variables would be VariableA, VariableB, VariableC, VariableD...

 

It's similar to Actionscript's

 

   this["q" + i + "_mc"]._x = this["dragTxt" + i]._x - 20;

   this["q" + i + "_mc"]._y = this["dragTxt" + i]._y + 14;

then the instance is q1_mc, q2_mc etc..

 

I remember that I could do similar things in Director but forgot how!

 

Thanks for the help,

 
Replies
  • Currently Being Moderated
    Mar 20, 2012 2:38 PM   in reply to wfzen
    do "Variable" & temp & " = " & 1
    
     
    |
    Mark as:
  • Currently Being Moderated
    May 17, 2012 4:51 AM   in reply to wfzen

    It would be helpful if you provide some background info about what you are trying to do. But does this do what you want?

     

    repeat with i in initList   

       put i & "String = " & value(i & "UserStatuses")

    end repeat

     
    |
    Mark as:
  • Currently Being Moderated
    May 17, 2012 9:59 AM   in reply to wfzen

    Are just wanting to display the list as a string in some text member? You don't have to create a whole new set of variables (ex Bstring) to hold the equivoloent of what is in the list variable (BUserStatuses). if BUserStatuses is a list like [0,0,0,0,0,0,0,] then string(BUserStatuses) is "[0,0,0,0,0,0,0,]". Just use string(BUserStatuses) wherever you were wanting to use BString.

     
    |
    Mark as:
  • Currently Being Moderated
    May 17, 2012 10:11 AM   in reply to wfzen

    Wow. Talk about not seeing the forest through the trees. There's never any good reason to create variables at runtime like that, regardless if there are ways to do it in the scripting language (which there invariably are). Your "on test" handler above uses a whole slew of lists to set up and create a bunch of "dynamic" variables when you should be stopping at lists and property lists (arrays and associative arrays) to do what you want, which is to store your data. The method you currently have conjured up is excessive at best, and bad programming practice at worst (See this link for some good discussion on why it should be avoided: http://programmers.stackexchange.com/questions/103083/how-can-variable s-be-created-at-runtime)

     

    So, I would do what you want like this:

    -- initialize the property list (associative array, in other scripting languages)

    gUserStatus = [:]

     

    -- add an entry

    gUserStatus["Bob"] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

     

    -- print the property array to the message window

    put gUserStatus

    -- ["Bob": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

     

    add a few more users:

    gUserStatus["Mary"] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

    gUserStatus["Sue"] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

    gUserStatus["Paul"] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

     

    -- get value from property list, based off name

    put gUserStatus["Bob"]

    -- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

     

    -- set value in value list, based off name, then put to message window

    gUserStatus["Bob"][3] = 1

    put gUserStatus["Bob"]

    -- [0, 0, 1, 0, 0, 0, 0, 0, 0, 0]

     

    so, I can add as many users as I want... as long as they're identified with unique properties (or keys, in other languages). I can even get the count or number of users:

    put gUserStatus.count

    -- 4

    I can easily iterate through them and get/set information:

    cnt = gUserStatus.count

    repeat with i = 1 to cnt

      put "Name: " & gUserStatus.getPropAt(i) & ", Status: " & gUserStatus[i]

    end repeat

     

    In your example you don't know how many users you have because your variables are being dynamically created at runtime. You could create yet another variable to keep track of that, but why when you have lists and property lists that are perfect structures for this type of associative data?

     

    There's a huge gap in your understanding how to structure, keep track of, and manipulate data, and it's important to fill it now by learning about lists and property lists (arrays and associative arrays). Doing it now will truly open up a whole new way of programming to you.

     
    |
    Mark as:
  • Currently Being Moderated
    May 25, 2012 1:59 AM   in reply to josh_chunick

    Lists and property lists can contain a variable number of items (normally you initialize them on prepareMovie handlers in movie scripts or on beginSprite handlers for lists that are needed in a given behavior script; remember a list can be also a global, local or behavior property). You can add/delete etc at runtime so that would solve your problem. In many of my projects I've made use of this concept which is I believe esential. As previous poster indicated (totally agree with him) it's the way to go and could open a whole new programming panorama for you.

     

    Just a sidenote: initialization of a list---> aList= []; initialization of a property list----> aPropList= [:]. This may be important at some point to avoid initial errors.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points