|
|
| 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.
This example would output something like the following.
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().
This example would output something like the following.
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.
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.
|
| Related Info: | DeleteArray | Dim | GetArray | ReDim | SetArray | UnDim : |
|
|
|||||||||||||||||||||||||||||||||||||||
|
(c) Copyright 2002 - 2026 - Kevin Picone - PlayBASIC.com |