marc_256 wrote:How to redim a multi array ?
Code: Select all
Global Dim MyArray.l(10, 5)
ReDim MyArray.l(20, 5)
Hello marc_256. Here's a workaround to preserve the contents of a two-dimensional array while re-dimensioning the first. For clarity, this example works on option base 1, but can easily be adapted to work with option base 0, as well as with more dimensions:
Code: Select all
Procedure ReDim_Preserve(Array xArray.s(2), newDim1, newDim2)
Dim tempArray.s(ArraySize(xArray(), 1), ArraySize(xArray(), 2))
For L = 1 To ArraySize(xArray(), 1)
For LL = 1 To ArraySize(xArray(), 2)
tempArray(L, LL) = xArray(L, LL)
Next LL
Next L
Dim xArray(newDim1, newDim2)
For L = 1 To newDim1
For LL = 1 To newDim2
If L > ArraySize(xArray(), 1) Or LL > ArraySize(xArray(), 2) Or
L > ArraySize(tempArray(), 1) Or LL > ArraySize(tempArray(), 2)
Break
Else
xArray(L, LL) = tempArray(L, LL)
EndIf
Next LL
Next L
FreeArray(tempArray())
EndProcedure
Procedure displayArray(Array xArray.s(2), header.s)
Debug header
Debug "---"
For L = 1 To ArraySize(xArray(), 1)
For LL = 1 To ArraySize(xArray(), 2)
Debug Str(L) + "," + Str(LL) + " = " + xArray(L, LL)
Next LL
Next L
Debug ""
EndProcedure
Define L, LL, dataNames.s
Dim myArray.s(2, 4)
For L = 1 To 2
For LL = 1 To 4
Read.s dataNames
myArray(L, LL) = dataNames
Next LL
Next L
displayArray(myArray(), "orignal myArray(2, 4):")
ReDim_Preserve(myArray(), 3, 5)
displayArray(myArray(), "after ReDim_Preserve(3, 5):")
ReDim_Preserve(myArray(), 1, 3)
displayArray(myArray(), "after ReDim_Preserve(1, 3):")
DataSection
Data.s "Albert Einstein", "Isaac Newton", "Charles Darwin", "Guglielmo Marconi"
Data.s "Galileo Galilei", "Leonardo da Vinci", "René Descartes", "Thomas Edison"
EndDataSection
Not exactly sophisticated code, but it does the job.
