Announcement

Collapse
No announcement yet.

Arrays of Arrays - Any way to implement?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Arrays of Arrays - Any way to implement?

    I have run into a problem solving a problem mentioned in an earlier post (the problem with multiple instantiations). My problem is I would like to create an array of arrays (something possible with VB but apparently not in PB). I need the ability to create (dynamically) x number of arrays of unknown sizes. For Example:

    I have a function which can be called an unknown number of times, and with each unique call of that function I need to set up some data storage. I would like to create a master Data-Storage array with each element being an array of actual data being stored. This approach would allow me to continually redim preserve the array allowing it to grow as needed.

    Code:
    	Val1 = MyFunc(5,Init)
    	Val2 = MyFunc(7,Init)
    	.
    	.
    	.
    	Val9 = MyFunc(25,Init)
    With each init call of this function, I would add a new element array of data to the master array.

    Code:
    	ReDim DataStorage(1:1)
    	ReDim DataStorage(1).(1:5)
    
    	ReDim Preserve DataStorage(1:2)
    	ReDim DataStorage(2).(1:7)
    
    	.
    	.
    	.
    
    	ReDim Preserve DataStorage(1:9)
    	ReDim DataStorage(9).(1:25)
    My question is, what is the best (and easiest) way to implement something like this in PowerBasic? Any and all suggestions/advice is welcomed!

    Thank you in advance for your help.

    Best regards,

    Andrew Peskin
    [email protected]





    ------------------

  • #2
    How about:
    Code:
    DIM ArrayStart(1 to 100) As Global Long '// Where the data starts
    DIM ArrayLen(1 to 100) As Global  Long   '// How much data there is
    DIM ArrayData(1 To 10000) As Global  Long '// The data array
    DIM g_Position As Global Long
    
    Sub AddData(index As Long, dataPTR As Long PTR, vCount As Long)
    Dim i As Long
    Dim v1PTR As Long Ptr
    Dim vStart As Long
    Dim v As Long
       vStart = g_Position + 1
       If (vStart + vCount) > UBound(ArrayData) Then
          v = UBOUND(ArrayData) + vCount + 1000
          Redim Preserve ArrayData(1 To v)
       End If
       If index > UBound(ArrayStart) Then
          v = index + 1000
          Redim Preserve ArrayStart(1 To v)
          Redim Preserve ArrayLen(1 To v)
       End If
       v1PTR = VARPTR(ArrayData(vStart))
       For i = 1 To vCount
          @v1PTR[i - 1] = @dataPTR[i - 1]   
       Next i
       ArrayStart(index) = vStart
       ArrayLen(index) = vCount
       g_Position = g_Position + vCount
    End Sub
    
    Function ReadArray(index As Long, element As Long) As Long
       If element > ArrayLen(index) Then
          msgbox "ERROR!!! Out of bounds!"
       End If
       ReadArray = ArrayData(ArrayStart(index) + element - 1)
    End Function
    
    '////
    Using the sub:
    Dim temp1(1 to 777) As Long
    Dim temp2(1 to 176) As Long
    Dim i As Long
    Dim v1PTR As Long Ptr
       For i = 1 to 777
          temp1(i) = Rnd * 3000
       Next i
       For i = 1 to 176
          temp2(i) = Rnd * 3000
       Next i
    
       v1PTR = VARPTR(temp1(1))
       AddData 1, v1PTR, 777
    
       v1PTR = VARPTR(temp2(1))
       AddData 2, v1PTR, 176
    
       i = ReadArray(1, 211) '// Read element 211 from array 1
       i = ReadArray(2, 122) '// Read element 122 from array 2

    [This message has been edited by Tim Wisseman (edited October 03, 2000).]

    Comment


    • #3
      I made an even easier example that does not use pointers

      Code:
      DIM ArrayStart(1 to 100) As Global Long '// Where the data starts
      DIM ArrayLen(1 to 100) As Global  Long   '// How much data there is
      DIM ArrayData(1 To 10000) As Global  Long '// The data array
      DIM g_Position As Global Long
      
      Sub MakeArray(index As Long, vSize As Long)
      Dim vStart As Long
      Dim v As Long
         vStart = g_Position + 1
         If (vStart + vSize) > UBound(ArrayData) Then
            v = UBOUND(ArrayData) + vSize + 1000
            Redim Preserve ArrayData(1 To v)
         End If
         If index > UBound(ArrayStart) Then
            v = index + 1000
            Redim Preserve ArrayStart(1 To v)
            Redim Preserve ArrayLen(1 To v)
         End If
         ArrayStart(index) = vStart
         ArrayLen(index) = vSize
         g_Position = g_Position + vSize
      End Sub
      
      Sub WriteArray(index As Long, vElement As Long, vValue As Long)
         ArrayData(ArrayStart(index) + vElement - 1) = vValue
      End Sub
      
      Function ReadArray(index As Long, vElement As Long) As Long
         ReadArray = ArrayData(ArrayStart(index) + vElement - 1)
      End Function
      '//
      '////Using the sub:
      '// I will make 4 arrays of 4 different sizes
      MakeArray 1, 177  'Array 1 has 177 elements
      MakeArray 2, 188
      MakeArray 3, 200
      MakeArray 4, 19
      '//
      '//
      '// I will write data to the array
      WriteArray 1, 56, 13  '// Write to array 1, set element 56 to a value of 13
      WriteArray 3, 199, 1
      '// Read data from the array
      i = ReadArray(1, 56)  '// Read array 1 element 56
      ------------------

      Comment

      Working...
      X