"If there are multiple dimensions, only the upper bound of only the first dimension may be changed safely. If the first dimension is reduced, the existing mappable data may be lost. If lower-order dimensions are resized at all, the effects can be hard to predict (because multidimensional arrays are stored in row-major order : values differing only in the last index are contiguous)."
I take from that -- don't bother with redim preserve in >=2D arrays.
So a few years ago I made up a cheesy method for preserving a 2D matrix of components which I want to expand or reduce.
I don't think anything in fbc-int/array.bi would be of help here.
Cheesy method:
Code: Select all
#macro redimpreserve(a,d1,d2)
Scope
Redim As Typeof(a) copy(d1,d2)
For x as long =d1
For y as long =d2
If x>=Lbound(a,1) And x<=Ubound(a,1) Then
If y>=Lbound(a,2) And y<=Ubound(a,2) Then
copy(x,y)=a(x,y)
End If
End If
Next y
Next x
Redim a(d1,d2)
For x as long= d1
For y as long=d2
a(x,y)=copy(x,y)
Next y
Next x
End Scope
#endmacro
'================== helpers only for demo ====================
#macro printout(d)
scope
print "(";Lbound(d,2);" to";Ubound(d,2);" ,";Lbound(d,1);" to "; Ubound(d,1);")"+chr(10)
dim as long ct
For n As Long=Lbound(d,1) To Ubound(d,1)
ct=0
For m As Long=Lbound(d,2) To Ubound(d,2)
ct+=5
#if typeof(d)=string
print "'";d(n,m);"'";tab(ct);
#else
Print d(n,m);tab(ct);
#endif
Next m
Print
Next n
Print
end scope
#endmacro
sub setstring(s() as string)
dim as long k,i,j
For i = lbound(s,1) To ubound(s,1)
For j = lbound(s,2) To ubound(s,2)
if s(i,j)="" then s(i, j) = chr(65+k)
k+=1
Next j
Next i
end sub
screen 21
Redim Shared As Double d(1 To 2,3)
d(1,0)=1:d(1,1)=2:d(1,2)=3:d(1,3)=4
d(2,0)=5:d(2,1)=6:d(2,2)=7:d(2,3)=8
printout(d)
redimpreserve(d,-2 To 5,-3 To 7)
printout(d)
redimpreserve(d,0 To 5,0 To 7)
printout(d)
redim as string s(3,4)
setstring(s())
printout(s)
redimpreserve(s,0 to 5,0 to 6)
printout(s)
print "Fill the empties"
setstring(s())
printout(s)
redimpreserve(s,0 to 4,0 to 5)
printout(s)
print "done"
sleep