Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Preserving multi-dim arrays
#1
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?

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
Reply


Messages In This Thread
Preserving multi-dim arrays - by Pete - 12-18-2025, 06:18 PM
RE: Preserving multi-dim arrays - by SMcNeill - 12-18-2025, 06:21 PM
RE: Preserving multi-dim arrays - by Pete - 12-18-2025, 07:00 PM
RE: Preserving multi-dim arrays - by Dimster - 12-18-2025, 09:13 PM
RE: Preserving multi-dim arrays - by Pete - 12-18-2025, 11:49 PM
RE: Preserving multi-dim arrays - by Dimster - 12-19-2025, 03:17 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Arrays inside Types? Ikerkaz 7 168 Yesterday, 04:21 PM
Last Post: ahenry3068
  Arrays as UDTs Pete 26 1,109 02-06-2026, 04:31 AM
Last Post: bplus
  Array out of passing arrays... Pete 2 391 09-22-2025, 08:53 PM
Last Post: ahenry3068
Question REDIM takes more resources than DIM? bplus 34 5,772 07-23-2024, 01:04 PM
Last Post: luke
  Arrays of record variables Kernelpanic 0 447 04-02-2024, 06:58 PM
Last Post: Kernelpanic

Forum Jump:


Users browsing this thread: 1 Guest(s)