Accessing groups of arrays by using GetArray and SetArray

Started by kevin, January 19, 2014, 11:22:38 PM

Previous topic - Next topic

kevin

 Accessing groups of arrays by using GetArray and SetArray

    So here's a seed example to get you started with this very old concept.   Arrays in PlayBASIC live in a table, just like variables, which is hardly unique behavior for programming language. Now in some lower level programming languages, we have access to a array pointers,  but PlayBASIC doesn't include such constructs, but it does include stub arrays.  A stub array is way to define an array that has no data of its own,  which is the primary difference to a normal array, which owns and protects one set of data within it.    They include a data type in the declaration, but don't include any size data (they inherit this from the source).   So you can pass a String Array to a Integer Array.   

  The GetArray  commands allow us to grab the 'Array Index'  from any array we query.   This is just an integer value, there's nothing all that special about it.  The value is just the position of the array within the main array table.   So if you declared threes arrays (of the same type) in your code, there indexes would be sequential.

 SetArray does the opposite, it lets a programmer copy the 'arrays data handle' from the source arrays index (the value we got from GetArray) into the array we nominate.   If that array is a stub array (defined by MakeArray) than all it does it copy the handle value (an integer) from the source array into the destination.   At that point, the stub is now identical to the source array.

   Since array indexes (of the same type) are sequential, this allows us to write loops that can run through and process information via a stub array.   This allows us to serialize repetitive blocks of code out.

   Bellow we're a list of array that might be used for lists of animation data say.   The code declares a bunch of animation arrays, computes the first Index and last indexes of this run of arrays and then uses as a good old For/Next loop to run through them.    So if add more declarations between the markers (STart_OF_Animations() and End_OF_Animations() )  the routine will seemingly handle them automatically.


PlayBASIC Code: [Select]
   // define marker array so we can work out 
makearray STart_OF_Animations()

// Conceptual Block of arrays to hold stuff like animation sequences
Dim Animation_0(10)
Dim Animation_1(20)
Dim Animation_2(30)
Dim Animation_3(40)
Dim Animation_5(50)

// define marker array as the end of our block
makearray END_OF_Animations()


FirstAnim=GetArray(STart_OF_Animations())+1
LastAnim =GetArray(End_OF_Animations())-1

MaxAnimations =LastAnim-FirstAnim

// Make the viewer stub array as integer
MakeArray Anim()


// here we have a loop that can view step througth the above sequences of arrays.
For lp=0 to MaxAnimations
SetArray Anim(),FirstAnim+lp
print GetArrayElements(Anim())
next


Sync
waitkey