CopyArray will clone the entire contents of the source array to another array. While the source and destination array must be of the same data type, you can copy any type of array you wish, including Integer,Floating point, String and even typed arrays. This can save the programmer having to manually move information.
FACTS:
* If the destination array previously contained information CopyArray will undim the destination prior to cloning the Source data into the Destination.
* CopyArray can be used upon Integer, Floating Point, String and Type arrays.
Mini Tutorial #1:
In this simple example, we'll create an integer array, fill it random values. Then transfer this array into another using Copyrray and then display them both together.
; Create two Arrays... Dim Table(10) Dim TableBackup(0) ; Fill the Table() Array with random numbers For lp=0 To 10 Table(lp) =Rnd(1000) Next lp ; Copy the entire contents of the Table() array into ; the TableBackup() array. CopyArray Table(),TableBackUp() ; Display Both the table() and TableBackup arrays For lp =0 To 10 Print Table(lp) Print TableBackup(lp) Next ; Show the Disp[lay and wait for a key press Sync WaitKey |
Mini Tutorial #2:
In this simple example, we'll create two typed arrays, fill one with random values. Then transfer that typed array into another using CopyArray and then display the copy.
; Declare a type Type Pos X,y,z EndType ; Dim two typed arrays Dim MyData1(10) As pos Dim MyData2(10) As pos ; Fill MyData1 with random data Print "Data in MyData Array #1" For lp =0 To GetArrayElements(MyData1().pos,1) MyData1(lp).x =Rnd(100) MyData1(lp).y =Rnd(100) MyData1(lp).z =Rnd(100) Print Str$(mYdata1(lp).x)+","+Str$(mYdata1(lp).y)+","+Str$(mYdata1(lp).z) Next CopyArray MyData1().pos, MyData2().pos Print "Data in MyData Array #2" For lp =0 To GetArrayElements(MyData2().pos,1) Print Str$(MyData2(lp).x)+","+Str$(MyData2(lp).y)+","+Str$(MyData2(lp).z) Next ; Display the Screen and wait for the user to press a key Sync WaitKey |
|
Example Source: Download This Example ; ======================================== ; CopyArray EXAMPLE ; ======================================== ; Create an Integer Array called "MyArray" containing a random number of elements Dim MyArray(RndRange(1,20)) Dim MyArrayBackup(0) ; Fill May Array Will random Values FillMyarray() ; Copy the current State of Myarray() into MyArrayBackup() CopyArray MyArray(),MyArrayBackup() Repeat Cls 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 " C = Randomly Change MyArray()" Print " B = Backup Myarray() To MyArrayBackup()" Print " R = Restore Myarray() From MyArrayBackup()" Print " Space = Exit" ; Check Select Lower$(Inkey$()) Case "c" ; If the user Pressed the 'c' key, Then Fill MyArray with random values. FillMyArray() Case "b" ; If the user pressed the "b" then copy Myarray() into MyArrayBackup() CopyArray Myarray(),MyArrayBackup() ; If the user pressed the "r" then copy MyarrayBack() Back into MyArray() Case "r" CopyArray MyarrayBackUp(),MyArray() 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$)) size=GetArrayElements(ThisArray(),1) For lp =0 To size Contents$=Contents$+Digits$(ThisArray(lp),3) If lp<size Then Contents$=Contents$+"," Next Print Contents$ Print "" EndFunction Function FillMyArray() ; Fill MyArray() with Random values between 0 And 1000 For lp=0 To GetArrayElements(MyArray(),1) MyArray(lp)=Rnd(1000) Next EndFunction |
|