7 Replies Latest reply on May 25, 2012 1:59 AM by nairkat

    Include variables with minor difference in a loop

    wfzen Level 1

      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,

        • 1. Re: Include variables with minor difference in a loop
          prossel
          do "Variable" & temp & " = " & 1
          
          • 2. Re: Include variables with minor difference in a loop
            wfzen Level 1

            Thanks for the answer. So how do I make the following work?

            The lists have only one difference, the first letter; I want to convert all of them to different strings. Instead of writing line by line. I'd like to create a initList with B, C, D, etc. and then just switching out hte first letter with repeats.

             

            I can't get the following to run. What is the syntax error?

             

            Thanks,

             

             

            on test

              BUserStatuses = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

              CUserStatuses = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

              DUserStatuses = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

              FUserStatuses = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

              HUserStatuses = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

              LUserStatuses = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

              MUserStatuses = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

              PUserStatuses = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

              SUserStatuses = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

              TUserStatuses = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

              VUserStatuses = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

              WUserStatuses = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

              initList = ["B", "C", "D", "F", "H", "L", "M", "P", "S", "T", "V", "W"]

              repeat with i = 1 to 12

                temp = initList.getAt(i)

                do temp & "String" = "string(" & temp & "UserStatuses"& ")"

                do "put" temp & "String"

              end repeat 

            end

            • 3. Re: Include variables with minor difference in a loop
              Mister MUX Level 2

              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

              • 4. Re: Include variables with minor difference in a loop
                wfzen Level 1

                Each list stores different data about different groups of people. I'd like to convert each one to a string and store it into a different variable (e.g. BString, CString, etc). I currently is doing a lot of repeating lines to get individual data.

                For example for B, I would add:
                BString = string(BUserStatuses)
                delete char 1 of BString
                delete the last char of BString
                ...

                till the end W.

                So I'm trying to use repeat for setting up variables with minor differences from each other, eg. variable1, varaible2, etc.  The script you have seems to output the text but didn't set the variables with values. If add do before it, all I got is <VOID>.

                 

                Thanks for the help,

                • 5. Re: Include variables with minor difference in a loop
                  Mister MUX Level 2

                  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.

                  • 6. Re: Include variables with minor difference in a loop
                    josh_chunick Level 1

                    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-variables-be-created-at-runt ime)

                     

                    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.

                    • 7. Re: Include variables with minor difference in a loop
                      nairkat Level 1

                      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.