Welcome to the SRP Forum! Please refer to the SRP Forum FAQ post if you have any questions regarding how the forum works.

Dynamic array assign

Maybe I am just having a Friday moment but is it possible to dynamically allocate an numbered array as a variable? Let me explain...

Basically I have multiple arrays I want to do the same set of routines on. I want to do something like:

BaseArray = "MY_ARRAY_" ArrayCnt = Dcount(......) For CurrArray = 1 to ArrayCnt FocusArray = BaseArray:CurrArray ; // Here I want FocusArray equal to the arrray MY_ARRAY_1, not the string. //Do some stuff here to FocusArray and then set it back, doing the reverse of the line above. Next CurrArray

The concept of BaseArray:CurrArray works with things like RowExists, Xlate, Read Rec, etc... because they want the string label, not the array itself.

In a nutshell I want to evaluate the concatentation before the assignment so that it then assigns the Array itself, not the string name it would otherwise automatically revert to....

Comments

  • Does this mean that you don't know how many "MY_ARRAY_" arrays you're going to have?
    In other words, the number of arrays is dependent on some other variable?
  • No, at least not with simple variables. Assuming this program is running in the event context of a form, you can store your arrays in UDPs instead:
    Set_Property(@Window, '@MY_ARRAY_1', Array1) Set_Property(@Window, '@MY_ARRAY_2', Array2)
    Then in your loop you would do something like this:
    BaseArray = "MY_ARRAY_" ArrayCnt = Dcount(......) For CurrArray = 1 to ArrayCnt FocusArray = Get_Property(@Window, '@' : BaseArray:CurrArray) //Do some stuff here to FocusArray and then set it back, doing the reverse of the line above. Next CurrArray
  • An alternative is to store your multiple arrays in a dimensioned array:

    Dim Arrays(100) ; // Either some arbitrary large number or a known number ArrayCnt = Dcount(......) For CurrArray = 1 to ArrayCnt FocusArray = Arrays(CurrArray) //Do some stuff here to FocusArray and then set it back, doing the reverse of the line above. Next CurrArray
  • edited September 2022

  • @AusMarkB
    Well at the moment it is actually 6. I worked around my problem by just replicating the code 6 times. However I wanted to trim the code, make it more maintainable, and future proof it a bit.

    @DonBakke
    Unfortunately is is not form based otherwise I could have done that. Using UDPs in forms is one of my favorite things to do! This is just a store procedure that I am trying to build up to do a little background maintenance as a side project. I guess I could always create a NDW Maintenance form that is not menu driven to control it....
  • @Opto_Will, I was basically just checking I understood before suggesting what Don has with the dimensioned arrays.
    You don't need a form that way, nor replication of the code.
  • However if you know how many there are and they're not too complex of an array themselves, you could simplify further by setting them all to a singular non-dimensioned array.

    BaseArray = MY_ARRAY_1:@fm: MY_ARRAY_2:@fm: MY_ARRAY_3:@fm: MY_ARRAY_4:@fm: MY_ARRAY_5:@fm: MY_ARRAY_6 ArrayCnt = Dcount(BaseArray, @fm) ; // of course you know this number already but a little bit of future proofing For CurrArray = 1 to ArrayCnt FocusArray = BaseArray<CurrArray> ; // Here I want FocusArray equal to the arrray MY_ARRAY_1, not the string. //Do some stuff here to FocusArray and then set it back, doing the reverse of the line above. BaseArray<CurrArray> = FocusArray Next CurrArray
  • @AusMarkB @DonBakke,

    I wrote and posted my reply before I saw your followups!
    Gives me something to recode over the weekend ;-)

    You are both Legends!
Sign In or Register to comment.