SwapArray |
SwapArray SourceArray(), DestArray() |
|
Parameters: SourceArray() = The array you wish to swap with DestArray() DestArray() = The array you wish to swap with SourceArray()
|
Returns: NONE |
|
SwapArray allows you to swap the entire contents of the source array with the desitination array. After the swap the destination array contains the source arrays contents and vice versa. You only limitation is that the source and destination array types must match, you can happily swap any type of array you wish, including Integer, Floating point, String and typed arrays.
FACTS:
* SwapArray exchanges array data via internal pointers, so no data is actually copied. Making this command very fast.
Mini Tutorial:
In this example, we'll create two integer arrays. Fill them, then display the arrays before and after the swap. We should see the data has swapped places
; Create two Arrays... Dim Table1(10) Dim Table2(10) ; Fill the Table1() Array with values from 0 to 10. For lp=0 To 10 Table1(lp) =lp Next lp ; Fill the Table2() Array with values from 1000 to 1010. Print "arrays before the Swap" For lp=0 To 10 Table2(lp) =1000+lp Next lp ; Display Both arrays.. For lp =0 To 10 Print "Table1()="+ Str$(Table1(lp))+" Table2()="+Str$(Table2(lp)) Next ; Swap the contents of the Table1() and Table2() arrays. SwapArray Table1(),Table2() ; Display Both arrays.. Print "Arrays After the Swap" For lp =0 To 10 Print "Table1()="+ Str$(Table1(lp))+" Table2()="+Str$(Table2(lp)) Next ; Show the Display and wait for a key press Sync WaitKey |
This program outputs two columns of data, Displaying the contents of table1() on the left and the Table2() array on the right.
The output will look something like this before the swap.
Table1() =0 Table2()=1000 Table1() =1 Table2()=1001 Table1() =2 Table2()=1002 Table1() =3 Table2()=1003 Table1() =4 Table2()=1004 Table1() =5 Table2()=1005 Table1() =6 Table2()=1006 Table1() =7 Table2()=1007 Table1() =8 Table2()=1008 Table1() =9 Table2()=1009 Table1() =10 Table2()=1010 |
After the SwapArray the data will look like this.
Table1() =1000 Table2()=0 Table1() =1001 Table2()=1 Table1() =1002 Table2()=2 Table1() =1003 Table2()=3 Table1() =1004 Table2()=4 Table1() =1005 Table2()=5 Table1() =1006 Table2()=6 Table1() =1007 Table2()=7 Table1() =1008 Table2()=8 Table1() =1009 Table2()=9 Table1() =1010 Table2()=10 |
|
|
Example Source: Download This Example ; ======================================== ; SwapArray EXAMPLE ; ======================================== ; Create an Integer Array called "MyArray" containing a random number of elements Dim MyArray(RndRange(1,20)) ; Create an Integer Array called "MyArrayBackup" containing a random number of elements Dim MyArrayBackup(RndRange(1,20)) ; Fill MyArray() With random Values FillMyarray() Repeat ; clear the screen to black Cls RGB(0,0,0) ; Display the Contents Of the Two Arrays. Print "Array Contents" ShowMyArray("Myarray()",Myarray()) ShowMyArray("MyArrayBackup()",MyArrayBackup()) ; Display Some Controls So The user can Experiment with the Print "" Print "KeyBoard Controls" Print "=================" Print " F = Fill MyArray() with random values" Print " S = Swap Myarray() With MyArrayBackup()" Print " Space = Exit" ; Check if the user presses a key. Select Lower$(Inkey$()) Case "f" ; If the user pressed the "F" key Then fill the Myarray() with random values. FillMyArray() Case "s" ; If the user pressed the "s" key Then swap the Myarray() and MyArrayBackup() arrays SwapArray Myarray(),MyArrayBackup() EndSelect ; Draw the Screen Sync ; Repeat Until The SpaceKey has been pressed Until SpaceKey() ; END The Program End Function ShowMyArray(Message$,ThisArray()) ; Compile the contents of our the MyArray into a String And display them. Print "" Print Message$ Print Make$("-",Len(message$)) If GetArrayStatus(thisArray())=true size=GetArrayElements(ThisArray(),1) For lp =0 To size Contents$=Contents$+Digits$(ThisArray(lp),3) If lp<size Then Contents$=Contents$+"," Next Else Contents$="Array Is Not Defined" EndIf Print Contents$ Print "" EndFunction Function FillMyArray() ; Fill MyArray() with Random values between 0 And 1000 If GetArrayStatus(MyArray())=true For lp=0 To GetArrayElements(MyArray(),1) MyArray(lp)=Rnd(1000) Next EndIf EndFunctio |
|