QB64.org Forum
Active Forums => QB64 Discussion => Topic started by: Dimster on January 29, 2021, 01:23:29 pm
-
Is there a way to pull together the name of an array from different elements in a running program. I realize dynamic arrays can change the size while a program is running but is there a way to have a dynamic name as well?
For example: I have data pulled in from different files whose volume of data grows over time. I have a simple math formula that calculated the Moving Average of that data. Time period for the moving average can vary and the names of files vary. So lets say the files are numbered based on data of different events. So I want the program to work on Event file 23 and compare the data's moving average over every 4th data point with every 10th data point. What I have been trying to do is create an array along the following:
Dim Mov4AveEvent23 ( 1 to 45) or Dim Mov10AveEvent23( 1 to 45)
Input EventNum
EventNum$ = str$(EventNum)
Input MovNum
MovNum$=str$(MovNum)
MovName$ = "Mov"
Dim MovName$ + MoveNum + EventNum$(1 to 45)
Not sure if we can't build an array name at all or it this way of going about it is all out to lunch?
-
At first I was thinking TYPE with an id for the name but TYPE does not do arrays, dang!
BUT! you can Join an array to make a giant string of it and then effectively store the array as a variable length string in the type so there! you have an array and it's name in one structure.
To unpack the string back to an array simply use Split1000. And of course numbers will need to be converted back and forth from strings, so how bad do you need the names again?
If these are small arrays we are talking about then should work fine though a little complicated.
TYPE Array
aName as string
array as string
END Type
Then need a Join$(array() as string, connectorCharacter$) Function to take an array of strings and make giant string.
Then Split1000(splitMe$, toArray$(), connectorCharacter$) SUB to break string to Array again.
Maybe another SUB to convert numeric array to string? and back again as well.
-
@bplus
so how bad do you need the names again?
It's more about the convenience of creating a descriptive array and reducing the number of Dim statements. I have 50 files, each with data items ranging from 300 to 3000 approx items. The arrays could be preDimmed as :
DimMov4Event1( 1 to LengthofRecord)
Dim Mov10Event1(1 to LengthofRecord)
Dim Mov4Event2(1 to LengthofRecord)
etc up to Dim Mov4Event50(1 to LengthofRecord) and Dim Mov10Event50(1 to LengthofRecord)
A lot of dimming and event then I'd setting aside memory for arrays which may not be used from one run of the program to another. I'd like to be able to just input the Event file to use and the moving average factor for comparison.
I'm not sure what Split1000 is and you are correct about the difficulty about converting a string to it's numeric value..."Dim Mov4Event1" is expecting Numeric Values whereas the way I have been trying to do it forces me to create String arrays. All the data is stored as a numeric.
Appears the old tried and true method of creating one all purpose Dynamic Array (or two) and just force it work with multiple calls to varying data is the best way forward.
-
Wouldn't this work best with just a simple redim array?
REDIM MovEvent(1 to LengthofRecord)
Then, no matter what the length of the record is, the singular array holds all the info when you need it to.
-
@Dimster
I am not understanding the particulars of your problem and I am also thinking de ja vue we've been here before :)
I can work up some examples if you you are using one particular numeric type? Please say which or say don't bother.
I think if I can get across to you what I am talking about you will see how you can apply this for you problem unless I completely misunderstand what you situation is.
I am thinking you want something like an array or arrays of variable lengths with array names as simple index numbers just wont do.
-
Just use REDIM, instead 0f DIM. REDIM is dynamic.
I recall, to save memory in QuickBASIC, I used $DYNAMIC at the stat of the program. That way, every array is a dynamic array. Well, I suppose unless you DIM any as STATIC, in the program.
Pete
-
Hi Guys - thanks very much for looking at this - and yes I have been struggling with this in the past and as in the past I have used a lot of the work arounds you've been suggesting.
It's not the size or resizing of the array that I'm trying to change. That part I do understand. It's the NAME of the array I'm trying to play with and alter in the coarse of the run of the program.
Once again the brick wall in front of me is getting bloodied from the head butts. Hey, that could explain these idiotic questions.
Thanks again for taking a look at this, i'll take it from here.
-
I knew building that wall would come in handy someday!!!!! :D
Changing the name? Have you tried using REDIM SHARED Personal_Pronoun$()? That one changes with the direction of the wind. Hey if you mean can Joe$() be changed to Donald$(), the Supreme Court let us all down, so no. The only way to get that done, as far as I know, is: REDIM Donald$(UBOUND(Joe$()): FOR i = 1 to UBOUND(Donald$): Donald$(i) = Joe$(i): NEXT. Oh, and I saved the best part 'til last... ERASE JOE$.
Edit: A way I haven't played around with is to try this with the _MEM stuff.
Pete