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
#2
Isn't the simplest way for multi-dim arrays:
Make a new array the size you want.
Copy over the data you want to preserve.
Resize old array
Copy Data back to old array (one single memcopy now, as the arrays are the same size and have the same indexes)
Delete/Clear new array.

It's not going to be quick, but it'll get the job done.  And if you're resizing mutli-dimensional arrays constantly, you're doing something wrong.
Reply
#3
That's what my demo does, Steve. 

As an alternative, I don't know if it would also be possible to do the same using _Mem. I would think that would be equally or more involved. I haven't worked up anything using that keyword, so I can't provide an example.

BTW - I hope the X-mas tree business is booming this year. I'd cut off my right arm to visit and help you with the trees, but then you'd be a little short handed.

MAGA Christmas!

Pete Big Grin
Reply
#4
Pete that's very similar to what I've been doing with Multidim arrays. I like the ability to capture multi characteristics of a specific item like Name, Age, Height and Weight but I don't often find I need to expand the array like adding Eye Color to the characteristics. But when I do, I find I do something more ham handed then either your code or Steve's. What I do find I more often need to do with Multi D's is sorting and most often its sorting one of the characteristics and not the lead specific item. Like sort the Names by Age, or by Height or by Weight. Lot more involved with that exercise, the simplest way I found was to convert strings to numeric, make your lead specific item a decimal value and add up all the rest and then do a sort. For example, a multi D array with Names as the lead may already be in alphabetical order or not but if you substitute a number for the name you'd get the first name as 1, the 2nd as 2 and so on. Then you develop your sort array. 

Code: (Select All)
Age = 29
Weight = 135
Height = 6
NameNum = 15
'Sort Order by Weight
SortArray = Weight + Height + Age + (NameNum * .01)
Print SortArray
'To find any of the other characteristics its a matter of subtracting the total of the ones you don't want
'so to find the Actual first value of Weight, then Weight = SortArray -(Age + Height)
Weight = SortArray - (Age + Height)
Print Weight
 
This example should have shown it was for SortArray(15) as the data was specifically for the 15th Name in the original multi D array.

This is also the basis of how I (ham handedly) approach an expansion for Eye Color. The color has to be numeric. Rather than a SortArray, Its a Dim NewArray (1 to 5). and ReplacementArray (1 to 20, 1 to 20, 1 to 20, 1 to 20, 1 to 20) I use the same math formula with the exception of adding in the eye color (1=Blue, 2=Green,6=Brown etc as in Qbasic) so Name 15 in the NewArray now carries the value of Age+Weight+Height+Eye Color +(NameNum*.01). I then break all the values out from  NewArray as I fill ReplacementArray. 

By keeping a copy of the NewArray  I can sort things whenever I want.
Reply
#5
@Dimster

Okay, but wouldn't something like this be easier?

Code: (Select All)
Width 80, 30: _Font 16
x$(1) = "Harry,35,180,Blue"
x$(2) = "Steve,52,400,Brown"
x$(3) = "Pete,70,165,Green"
x$(4) = "Fred,40,175,Blue"
x$(5) = "Jim,61,205,Brown"

Print "By Name:"
For i = 1 To 5
    For j = i To 5 - 1
        q$ = LCase$(Left$(x$(i), 1))
        r$ = LCase$(Left$(x$(j + 1), 1))
        If q$ > r$ Then Swap x$(i), x$(j + 1)
    Next
Next
For i = 1 To 5
    Print x$(i)
Next
Print

Print "By Age:"
For i = 1 To 5
    For j = i To 5 - 1
        age1 = Val(Mid$(x$(i), InStr(x$(i), ",") + 1))
        age2 = Val(Mid$(x$(j + 1), InStr(x$(j + 1), ",") + 1))
        If age1 > age2 Then Swap x$(i), x$(j + 1)
    Next
Next
For i = 1 To 5
    Print x$(i)
Next
Print

Print "By Weight:"
For i = 1 To 5
    For j = i To 5 - 1
        seed1 = InStr(x$(i), ",") + 1
        wt1 = Val(Mid$(x$(i), InStr(seed1, x$(i), ",") + 1))
        seed2 = InStr(x$(j + 1), ",") + 1
        wt2 = Val(Mid$(x$(j + 1), InStr(seed2, x$(j + 1), ",") + 1))
        If wt1 > wt2 Then Swap x$(i), x$(j + 1)
    Next
Next
For i = 1 To 5
    Print x$(i)
Next
Print

Print "By Eye Color:"
For i = 1 To 5
    For j = i To 5 - 1
        ec1$ = Mid$(x$(i), _InStrRev(x$(i), ",") + 1)
        ec2$ = Mid$(x$(j + 1), _InStrRev(x$(j + 1), ",") + 1)
        If ec1$ > ec2$ Then Swap x$(i), x$(j + 1)
    Next
Next
For i = 1 To 5
    Print x$(i)
Next

Pete
Shoot first and shoot people who ask questions, later.
Reply
#6
Yes Pete, that method is easier for sorting string values which I assume the numeric values in your example are strings. But is the array a multi D?  In fact, even if it isn't, it does a great job of using multi data elements.

 I like the use of numeric values for the accuracy of formula results, I haven't been able to trust string conversions to numeric values mostly because I'm often mis-converting and winding up with a zero value . for example I'll try something like Bob = val(Bob$) whereas Bob = 45 has a level of accuracy that I can depend on.

But that being said, sorting is challenging and I like your method of a string array which captures all the elements of a multi D. Thanks for that.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Arrays inside Types? Ikerkaz 7 166 Yesterday, 04:21 PM
Last Post: ahenry3068
  Arrays as UDTs Pete 26 1,104 02-06-2026, 04:31 AM
Last Post: bplus
  Array out of passing arrays... Pete 2 389 09-22-2025, 08:53 PM
Last Post: ahenry3068
Question REDIM takes more resources than DIM? bplus 34 5,768 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)