12-18-2025, 06:18 PM
Since _Preserve is said to only work with single-dim arrays, what do you guys think about this method for working with multi-dim arrays when changing the number of array elements?
@Dimster
This is basically how I handled it before _Preserve came along for single and multi-dim arrays. If you have put together other methods, it might be interesting to compare them here.
Pete
Code: (Select All)
ind1 = 1: noa = 5
ReDim array$(ind1, noa)
For i = 1 To 5
array$(ind1, i) = "Pete-" + LTrim$(Str$(i))
Next
GoSub display
Do
Print "Press 1 to add to an array or 2 to delete from an array: ";
Do
_Limit 30
b$ = InKey$
If Len(b$) Then
If b$ = "1" Then AddDelete = 1: Print b$
If b$ = "2" Then AddDelete = -1: Print b$
If b$ = Chr$(27) Then End
End If
If AddDelete Then
If AddDelete = 1 Then Line Input "Name of array element: "; MyInput$
If AddDelete = -1 Then
Line Input "Press 0 to delete last added array or pick a number: "; MyInput$
If Val(MyInput$) Then ArrayNumber = Val(MyInput$): AddDelete = -2
End If
GoSub Add_Delete
GoSub display
Exit Do
End If
Loop
AddDelete = 0: Print
Loop
Add_Delete:
Select Case AddDelete
Case 1 ' Add.
noa = noa + 1: x$ = ""
ReDim temp$(ind1, noa)
For i = 1 To noa - 1
temp$(ind1, i) = array$(ind1, i)
Next
ReDim array$(ind1, noa)
For i = 1 To noa
hold$ = temp$(ind1, i)
array$(ind1, i) = x$
x$ = hold$
Next
x$ = "": array$(ind1, 1) = MyInput$: Erase temp$
Case -1 ' Delete.
If noa > 0 Then
ReDim temp$(ind1, noa - 1)
For i = 1 To noa
temp$(ind1, i - 1) = array$(ind1, i)
Next
noa = noa - 1
ReDim array$(ind1, noa)
For i = 1 To noa
array$(ind1, i) = temp$(ind1, i)
Next
Erase temp$
End If
Case -2 ' Delete numbered array.
noa = noa - 1
ReDim temp$(ind1, noa)
For i = 1 To noa
If i >= ArrayNumber Then temp$(ind1, i) = array$(ind1, i + 1) Else temp$(ind1, i) = array$(ind1, i)
Next
ReDim array$(ind1, noa)
For i = 1 To noa
array$(ind1, i) = temp$(ind1, i)
Next
Erase temp$
End Select
Return
display:
Cls
For i = 1 To noa
Print i; array$(ind1, i)
Next
Return
@Dimster
This is basically how I handled it before _Preserve came along for single and multi-dim arrays. If you have put together other methods, it might be interesting to compare them here.
Pete


