Copying


WARNING FOR BEGINNERS: If you are assigning the value of a variable to another variable in Red,

>> var1: var2        ;only if you are sure about it 

>> var1: copy var2   ;may save you hours of debugging

I have learned this the hard way, after hours trying to figure out why that field deep inside a GUI object was "magically" changing it's value.

copy

Creates a copy from a variable that does not point to the same original data. It creates a new copy of the data for the new variable to point at.

It may be used to copy series and objects.

It is not used on single items such as: integer! float! char! etc. For these, we can simply use the colon.

First lets look at a simple assignment:

>> s: [ "cat" "dog" "fox" "cow" "fly" "ant" "bee" ]
== ["cat" "dog" "fox" "cow" "fly" "ant" "bee"]
>> b: s
== ["cat" "dog" "fox" "cow" "fly" "ant" "bee"]
>> take/part s 4
== ["cat" "dog" "fox" "cow"]
>> b
== ["fly" "ant" "bee"]

Now with copy:

>> s: [ "cat" "dog" "fox" "cow" "fly" "ant" "bee" ]
== ["cat" "dog" "fox" "cow" "fly" "ant" "bee"]
>> b: copy s
== ["cat" "dog" "fox" "cow" "fly" "ant" "bee"]
>> take/part s 4
== ["cat" "dog" "fox" "cow"]
>> b
== ["cat" "dog" "fox" "cow" "fly" "ant" "bee"]

If you have a nested series (e.g. a block within your series) copy does not change the reference to these nested series. If you want to create an independent copy in this case, you must use the refinement /deep to create a "deep" copy.

Refinements:

copy also allows the refinements:

  • /part - limit the length of the result, where length is a number! or series!
>> a: "my house is a very funny house"
>> b: copy/part a 8
== "my house"
  • /types - copy only specific types of non-scalar values.

results matching ""

    No results matching ""