set variable value when variable name is stored in array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dsatino
    Contributor
    • May 2010
    • 393

    set variable value when variable name is stored in array

    I'm sure I've actually done this before, so why I can't figure this out now is annoying. I'm hoping someone can spark my memory here.

    Basically, I have 54 variable names stored in an array. I want to set the values of the variable values using the array as the name reference.

    In other words, instead of:
    x=1

    I want to set x by referring to it's location in the array.
    I tried:
    myarray(0,0)=1

    Unfortunately, this just changes the variable name that I have stored in the array to 1. Ha! Not quite what I want.

    Any help would be greatly appreciated.
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    I cannot understand why you would use a Multi-Dimensional Array, but in any case, in the following example, the Value of X would be equal to whatever is stored in the 3rd and 6th Elements at the intersection of the 1st and 2nd Dimension of the Array MyArray (assuming the Array is dimensioned starting at 0):
    Code:
    X = MyArray(2, 5)

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32662

      #3
      I looked at using Eval(), but that doesn't seem to do the business.

      As variables are not generally objects either, even populating your array with object references beforehand (in place of the simple names of the variables) would not work for you. Sorry. I'm stumped.

      Much easier in C derivative languages of course.

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        Sorry that I misread this Thread. Why not simply have a Hidden Lookup Table with Strings representing Variable Names along with/without associated Values? Setting/Retrieving Values would be a simple task. If you don't mind me asking, what is the purpose of this?

        Comment

        • dsatino
          Contributor
          • May 2010
          • 393

          #5
          The purpose, originally, was to set variables without having to type them. There are about 60 of them that get set at various stages of 3 nested loops. Depending on user inputs some may not even be used. The game plan to have it all neat and organized so that each variable would have a set location in the array that would align with the loop variables and hence, set the correct variable at the correct time.

          However, after coming in this morning with a fresh cup of coffee and a few hours of not trying to force this to work, it occurred to me that the array could just as easily store the variable values. (isn't that what arrays are for anyway? duh.)

          In any case, thanks all for your time and efforts as usual.

          P.S. I did find a previous project where I thought I was doing this and it turns out that I was calling functions. I also found documentation that stated that what I was trying to do was not possible with the VBA version of eval().

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32662

            #6
            This may seem somewhat radical, but have you considered using the array instead of the variables completely? It may not be an appropriate solution across the board, so only you can determine how much sense is in that.

            Comment

            • dsatino
              Contributor
              • May 2010
              • 393

              #7
              Yup, that's exactly what I meant when I said I would use the array to store the variable values instead of the variable names. So the variables themselves will cease to exist and the array will store all values with each specific location in the array relating to a certain point in the nested loops.

              Comment

              • NeoPa
                Recognized Expert Moderator MVP
                • Oct 2006
                • 32662

                #8
                Oh I see. Sorry. I misunderstood you.

                Good idea though :D

                Comment

                • agbb0cg
                  New Member
                  • Jul 2010
                  • 8

                  #9
                  Is this what you need to do?

                  Code:
                  <script type="text/javascript">
                    var a = "test"
                    var arr = new Array("a");
                    alert(window[arr [0]]);
                    window[arr [0]] = 'value changed';
                    alert(window[arr [0]]);
                  </script>

                  Comment

                  • NeoPa
                    Recognized Expert Moderator MVP
                    • Oct 2006
                    • 32662

                    #10
                    I'm afraid Access uses VBA exclusively. Javascript couldn't even run within Access.

                    Comment

                    Working...