Notifications
Clear all

[Closed] Copy Array Method

Hi,

Can someone please explain to me how the copy method for arrays works?

I’ve looked through Help and it says it returns Ok, and it only takes in 1 argument, which is the array you want to copy. How then do you copy the array as another array?

Thanks

6 Replies

It sounds like you want to duplicate an array, not copy it. Copying just makes references to the orginal array values, not copies of the actual values. Try this:

destinationArray = #()
for i=1 to sourceArray.count do append destinationArray sourceArray[i]

where destinationArray is the name of the array you want to create, and sourceArray is the name of the array you want to duplicate.

edit:Basically, I don’t have the slightest clue as to how copy <array> works, this is just how I do it. From the description in the help, it would seem oddly narrow in it’s purpose.

Hi handiklap,

Thanks for the reply. Yes, I’ve ended up having to write my own function which loops through the array and assigns each value to the corresponding bucket of the new array. I wanted to see if I could do without writing my own function and use the built in copy method, but it seems like I’m not the only one that’s scratching their heads on how the method works!

Thanks again

hey vasquez888,

Just for another solution to the same problem, whenever I make copies of arrays I find that using


 newArray = for i in oldArray collect i
 

is generally faster than


 newArray = #()
 for i in oldArray do append newArray i
 

especially with large array sizes. This is because internally, the entire array is copied to another memory location where the append can take place, then reassigned back to its old location everytime an append is called. Looping through an array with a for loop and collect will simply grab the array item directly and build a new array.

*edit: Sorry, I forgot to say that this way of copying an array creates an entirely new instance that is independent of the original array. This is usually what you want though

And then came Max 9

[left]deepCopy <array>
[/left]
[left][color=red]NEW in [/color][color=red]3ds Max [/color][color=red]9[/color][color=red]:[/color]Creates a copy of all elements in the array, including nested sub-arrays and returns the new array as the result.
[/left]

Hey Bobo, that’s a good one.

Light

Thanks for the helpful responses guys! Really appreciated!