Let's say I have a class array called Pets. Let's say Pets is the end-result of a huge, CPU-intensive sort that I'd rather not have to perform again, when I can simply cherry-pick what I need from the already-sorted Pets array.
So I create another class array of the same type, called Pets_Reptiles. Certain elements from the Pets array will be copied to the Pets_Reptiles array.
Without including the details of array resizing or instantiating, the pseudo code for copying what I want to be in this new array might generally be something like:
For Counter1 = 0 to Pets.Max
If Comp(Pets[Counter1].TypeOfAnimal,"reptile",gb.IgnoreCase) = 0 Then 'if the text contained in .TypeOfAnimal is 'reptile' then
Pets_Reptiles[Counter2] = Pets[Counter1] 'copy that element to an element in Pets_Reptiles
Counter2 = Counter2 + 1
EndIf
Next TheCounter
This will make a "shallow copy" of the data in certain elements from Pets, and assign them to Pets_Reptiles. The problem with this is any change made to an element in Pets_Reptiles is also made to the originating element from the Pets array. Because with a shallow copy, its not really a true copy. Its simply a pointer to each original element in the original array. If the program is continuing to use the Pets array for something completely different, changes having been made to Pets_Reptiles can create chaos/confusion
In Gambas 3.6.0, an option to do a "deep copy" .Copy was introduced. My understanding is that it creates a true, separate, independent copy in which changes to elements in the deep copy array have no effect on the originating array.
I can't find any examples of how to use the deep copy. I tried
Pets_Reptiles[Counter2] = Pets.Copy[Counter1]
and I got the error "not an object". So I'm at a loss of how to use .Copy or if it will even work on a class array. Can anyone alter my pseudo code to demonstrate the proper syntax of a deep copy?Thanks for reading.