Code: Select all
Macro NewDim(counter,size)
Dim MSV#counter.s(size)
MSV#counter(size)="Result"
Debug MSV#counter(size)
EndMacro
foo.i=0 : bar.i=0
NewDim(foo,bar)
Code: Select all
Macro NewDim(counter,size)
Dim MSV#counter.s(size)
MSV#counter(size)="Result"
Debug MSV#counter(size)
EndMacro
foo.i=0 : bar.i=0
NewDim(foo,bar)Code: Select all
Structure MSV_Array
Array MSV.s(0)
EndStructure
NewMap MSV_Array.MSV_Array()
Procedure NewDim(counter, size)
Shared MSV_Array()
AddMapElement(MSV_Array(), "MSV" + Str(counter))
Dim MSV_Array()\MSV(size)
MSV_Array()\MSV(size)="Result"
Debug MSV_Array()\MSV(size)
EndProcedure
foo.i = 0: bar.i = 0
NewDim(foo, bar)
foo = 2: bar = 10
NewDim(foo, bar)
Debug "0 " + MSV_Array("MSV0")\MSV(0)
Debug "2 " + MSV_Array("MSV2")\MSV(10)
Debug "5 " + MSV_Array("MSV5")\MSV(0) ;doesn't exist (reference uses default declared size)
Debug "3 " + MSV_Array("MSV3")\MSV(0)
Debug "95 " + MSV_Array("MSV95")\MSV(0)

The key to it all is really the structure containing the array. Here is a slightly less complicated version using arrays of arrays. It only uses an index# instead of a custom name and would be a little simply and faster because a hash of the array's name wouldn't have to be made (for lookup in a map). It also allows the side benefit of being able to access more than one of the arrays at the same time (although this could be done with the former map example using pointers).All in all, your modification provides much more readable and flexible albeit a bit more complicated (on the face of it) solution, as well as exemplifies that basic principle behind PureBasic data manipulation (that I agree with) which states — if you want to do something really subtle with PB arrays, use arrays together with maps, and with linked-lists, and with structures. Not to mention pointers
Code: Select all
Structure MSV_Array
Array MSV.s(0)
EndStructure
Dim MSV_Arr.MSV_Array(0)
Procedure NewDim(counter, size)
Shared MSV_Arr()
If ArraySize(MSV_Arr()) < counter
ReDim MSV_Arr.MSV_Array(counter)
EndIf
Dim MSV_Arr(counter)\MSV(size)
MSV_Arr(counter)\MSV(size)="Result"
Debug MSV_Arr(counter)\MSV(size)
EndProcedure
foo.i = 0: bar.i = 0
NewDim(foo, bar)
NewDim(2, 10)
Debug "0 " + MSV_Arr(0)\MSV(0)
Debug "2 " + MSV_Arr(2)\MSV(10) ;this will also create a default size for an array at index 1
Debug "1 " + MSV_Arr(1)\MSV(0) ;not formally declared and it's base element is still empty