MakeArray
MakeArray ArrayName()
 
Parameters:

    ArrayName() = The name of your pointer array
Returns: NONE
 

     MakeArray declares what are we call redirection array stub. Redirection arrays are like a retarget-able interface for arrays. While they don't have any of their own data, they're used to indirectly access other arrays data, much like passing an array into a functio. This can be achieved by grabbing an arrays handle (See: GetArray) and assign them to a redirection array (see:SetArray). Once assigned, the redirection array behaves just like the original array. This can be invaluable when you have many dimmed arrays, and need some way of accessing them universally using passing them through functions.

      When we declare a redirection array with MakeArray, we only need to assign it an array name (ie. MyArray() ), it doesn't require any dimensional sizing at this point, like Dim does. This is because it's not creating any data for itself, MakeArray is merely adding this Array Name to PlayBASIC's internal list of array declarations.

      When arrays are declared during compilation, using either DIM or MakeArray they're all placed into an array declaration stack. So each unique array has a Index on this stack. (You can even iterate through them) What redirection arrays allow us to do, is manually point our redirect array to look at other arrays that are listed within the array declaration stack.

      Once a redirection array is pointing at the data of another array, it effectively becomes that array. Until it's either redirected again, or it's deleted (using DeleteArray). Any changes you make using the redirection array, will also appear in the original data. Since the redirection array is not a copy of the original, but a interface to indirectly access the original arrays data.

      Redirection arrays receive, well inherit, they're dimensions from the first time the are used after being created. So they work virtually the same way passed Arrays do (passed into functions). Except we can use them without functions.





FACTS:


      * Redirection Arrays inherit their dimensions from the first time they're used after being declared.

      * Redirection Arrays can only be redirected to point at arrays of the same data type and number of dimensions.



I N D E X:






Mini Tutorial #1 : Iterating Through Arrays


      This example uses a Redirection Array to fill various integer arrays with random numbers.

      The example is really three parts.

      * Part #1, dimensions our four 1D integer arrays. Each array is a different size but the same dimensions (1D).

      * Part #2, creates another array called ArrayIndexes(), which is used to store the array indexes of the previously four created arrays.

      * Part #3, Define a redirection array called RedirectArray()

      * Part #4, We now step through the ArrayIndexes() array and use the stored handles to redirect our RedirectArray() array. This effectively allows use to iterate through and change our original arrays anyway we see fit.


  
  
; ==========================================
; Part 1 - Create the Four Integer Arrays
; ==========================================
  
; Create some Integer Arrays
  Dim Table1(1)
  Dim Table2(5)
  Dim Table3(7)
  Dim Table4(10)
  
  
; ========================================================
; Part 2 - Store the Array Handles in Table Array
; ========================================================
  
  
; Create an array to hold above typed array Indexes
  Dim ArrayIndexes(4)
  
  
; get the typed array index of Table1() and store in ArrayIndexes(1)
  ArrayIndexes(1)=GetArray(Table1())
  
; get the typed array index of Table2() and store in ArrayIndexes(2)
  ArrayIndexes(2)=GetArray(Table2())
  
; get the typed array index of Table3() and store in ArrayIndexes(3)
  ArrayIndexes(3)=GetArray(Table3())
  
; get the typed array index of Table4() and store in ArrayIndexes(4)
  ArrayIndexes(4)=GetArray(Table4())
  
  
; ========================================================
; Part 3 - Declare a Integer Redirection Array to access the
;          other integer arrays
; ========================================================
  
; Make (Declare) a redirection array called "RedirectArray"
  MakeArray RedirectArray()
  
  
; ========================================================
; Part 4 - Run through and display the array contents
; ========================================================
  
; Run through the listed arrays
  For ThisArray=1 To 4
     
   ; Set RedirectArray() to use the data located in one
   ; of the ArrayIndexes() table
     
   ; After setting the pointer array,  RedirectArray
   ; takes on the role of being the array it points at.
     SetArray RedirectArray(),ArrayIndexes(ThisArray)
     
   ; Get the size of this array
     size=GetArrayElements(RedirectArray(),1)
     
   ; Display the size of this array
     Print "Array Size:"+Str$(size)
     
   ; Run through the current RedirectArray() and show it's contents
     t$=""
     For Lp=1 To size
      ; put a random value in this array
        RedirectArray(Lp)=Rnd(100)
        
      ; add the contents of this cell to a T$
        t$=t$+Str$(RedirectArray(lp))+","
     Next Lp
     
   ; display the total contents of this array held in the T$
     Print TrimRight$(t$,",")
     
     Print ""
  Next
  
; Display the screen and wait for a key press
  Sync
  WaitKey
  
  
  



This example would output something like the following.

  
  Array Size: 1
  23
  
  Array Size: 5
  92,34,22,64,36
  
  Array Size: 7
  86,78,95,75,72,85,49
  
  Array Size: 10
  79,69,8,55,92,26,48,84,65,70
  
  



Top









Mini Tutorial #2 : Iterating Through Arrays


      This example does the same as the previous example (iterates through and fills some arrays using redirection) but takes a different approach.

      The example is really two parts.

      * Part #1, dimensions our four 1D integer arrays. Each array is a different size but the same dimensions.

      * Part #2, replies upon the previously defined table arrays being dimensioned in order. This means that the Table1(),Table2(), Table3() & Table4() arrays will be defined and stored within the Array Declaration stack sequentially. Since we know this, we can use this to our advantage and subsequently skip over the need to store our array pointers in a look up array (as per the first example). So now we can create valid Array Pointer index, by just getting the pointer to the first defined array and then adding an offset to it.

      So to access Table1(), we can create our own ArrayPointerIndex by adding the base arrays pointer i.e GetArray(Table1() with an offset of 0,

      To access Table2(), we'd grab our base arrays pointer i.e GetArray(Table1() and add an offset of 1 to it. since Table2() was the next array declared after Table1()

      To access Table3(), we'd grab our base arrays pointer i.e GetArray(Table1() and add an offset of 2 to it. since Table3() was declared after Table2(), so it's 2 index's ahead of table1().

      To access Table4(), we'd grab our base arrays pointer i.e GetArray(Table1() and add an offset of 3 to it. since Table4() was declared after Table3() , it's 3 index's ahead of Table1().




  
; ==========================================
; Part 1 - Create the four Integer Arrays
; ==========================================
  
; Create some integer Arrays,
  Dim Table1(1)
  Dim Table2(5)
  Dim Table3(7)
  Dim Table4(10)
  
  
; ========================================================
; Part 2 - Declare a redirection Array to access the other array data
; ========================================================
  
; Make (Declare) a redirection array  called "RedirectArray"
  MakeArray RedirectArray()
  
  
  
; Run through the listed arrays
  For ThisArray=0 To 3
     
   ; Since declared arrays are stored in a stack, and
   ; Array Pointers are really the index of this array
   ; within the array stack, this means that we can actually
   ; step through sets of arrays that were declared in a
   ; order. (as above Table1(),Table2(),Table3(),Table4())
     
     
   ; So this code gets the Array Index of TABLE1()
   ; then adds the 'ThisArray" loop counter to it.
     ArrayIndex=GetArray(Table1())+ThisArray
     
   ; Set RedirectArray to use this array index
     SetArray RedirectArray(),ArrayIndex
     
   ; Get the size of this array
     size=GetArrayElements(RedirectArray(),1)
     
   ; Fill this Array with random values
     Print "Array Size:"+Str$(size)
     
   ; Run through the current array RedirectArray
   ; store some random numbers in this array
     t$=""
     For Lp=1 To size
      ; place random value at this location in the array
        RedirectArray(Lp)=Rnd(100)
        
      ; Add the data from this cell to a T$ string
        t$=t$+Str$(RedirectArray(lp))+","
     Next Lp
     
   ; print t$ (the contents of this array)
     Print TrimRight$(t$,",")
     Print ""
  Next
  
; Display the screen and wait for a key press
  Sync
  WaitKey
  
  





This example would output something like the following.

  
  Array Size: 1
  23
  
  Array Size: 5
  92,34,22,64,36
  
  Array Size: 7
  86,78,95,75,72,85,49
  
  Array Size: 10
  79,69,8,55,92,26,48,84,65,70
  
  


Top






Mini Tutorial #3 : Raw Array Handles


      In this example we're using array handles directly. Array handles are generally hidden from to the user, except when we pass them out of functions. So we can use that to our advantage to create some pretty crazy custom data structures, but first, let's just get used to the idea.

      Bellow we're defined a couple of arrays, which are then passed through our custom GetIntegerArrayHandle function to return the handle of the passed integer array. The handles are just integer values, which the runtime uses to locate where memory resources such as arrays are physically stored. The handle never changes, but the data that they refer do routinely does. So it's much safer than using raw pointers.

     Once we've an array handle, we can then it assign to our redirection / stub array. When we do that, any access of the stub array will as if we were accessing the other original array. The difference is the redirection / stub() doesn't own the data. So we can use this method to create arrays of arrays or example, or evan arrays of arrays of arrays etc. The benefit of this structure is that it's without the overhead of the nesting.

  
; ------------------------------------------
; Raw Array Handles
; ------------------------------------------
  
  Dim Table1(10)
  Dim Table2(20)
  
; Get the internal array handles of our arrays
  Table1Handle=GetIntegerArrayHandle(Table1())
  Table2Handle=GetIntegerArrayHandle(Table2())
  
  
  
; define our redirection stub array
  MakeArray Stub()
  
  
  
;  Place the raw handle of Table1() into our stub
; the Stub() is now
  Stub()  = Table1Handle
  
; display the size of the stub now
  Print GetArrayElements(Stub())
  
  
;  Place the raw handle of Table2() into our stub
; the Stub() is now
  Stub()  = Table2Handle
  
; display the size of the stub now
  Print GetArrayElements(Stub())
  
  
  Sync
  WaitKey
  
  
  
; This Function returns the
; the returned arrays just come back as integers
; the integer is the array taw handle inside the
; runtime.   You can write these handles into
; redirection/stub arrays
  
Function GetIntegerArrayHandle(ThisArray())
EndFunction ThisArray()
  
  


Top







Mini Tutorial #4 : Array Handles - Array Of Arrays


      In this example we're creating a conceptual array of arrays structure. So we're going to use a 1D integer array as a container. This container will only hold other array handles as list of integers. So we're creating a type of conceptual 2 dimensional array here. The difference to a normal 2D array, is that our conceptual version can hold arrays with different lengths of the data in each row. So the row arrays can be expanded, contracted, deleted separately.

      Now in these example, we're using integer arrays, but this also works with floats/strings and typed arrays/lists.




  
  
;----------------------------------------------------
; Array Of Arrays Example
;----------------------------------------------------
  
  Size=40
  
  // Declare an integer array to hold array handles
  Dim Container(Size)
  
  
  // Fill our custom container array with array handles
  // of random size and random contents
  For lp=0 To Size
     
     // Create an array of random size
     // and fill it with some random integers
     COntainer(lp)=AllocIntegerArray()
  Next
  
  
  
  // ---------------------------------------------
  // Show the contents of our container array
  // ---------------------------------------------
  MakeArray Buffer()
  
  For lp=0 To Size
     If Container(lp)
        
        // place this handle in our stub array
        // now we can access this like it's normal integer array
        Buffer() =Container(lp)
        
        
        // Display the contents of this array
        s$="Array ["+Digits$(lp,3)+"] = "
        For ScanLp=0 To GetArrayElements(Buffer())
           
         ; grab the value, convert it to a string and
         ; add it to our string of values in this array
           s$+=Str$(Buffer(ScanLP))+","
        Next
        
      ; display the contents
        Print TrimRight$(S$,",")
        
     EndIf
     
  Next
  
  
  
  // ---------------------------------------------
  // Delete the contents of our container
  // ---------------------------------------------
  For lp=0 To Size
     
   ; check if a handle at this position ??
     If Container(lp)
        
      ; handles are none zero, so there appears to
      ; one here.
      ; Now we place this handle in our stub array
        Buffer() =Container(lp)
        
      ; then justr call Undim to release the memory.
        UnDim Buffer()
        
      ; now we put a zero back our container to delete it
        Container(lp)=0
        
     EndIf
  Next
  
  
  
  
  
  // refresh the display and wait for a key press
  Sync
  WaitKey
  
  
  
  
  
  
Function AllocIntegerArray()
  
; Pic a size of this array at random, just make
; sure it's  between and 5 and 25
  SizeOfArray = RndRange(525)
  
; Create a new array
  Dim ThisArray(SizeOfArray)
  
  For lp=0 To SizeOfArray
     // fill array with random integers
     ThisArray(lp)=Rnd(10000)
  Next
  
; Return this back to the caller
EndFunction ThisArray()
  
  
  


Top








 
Related Info: DeleteArray | Dim | GetArray | ReDim | SetArray | UnDim :
 


(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com