Copying



 WARNING FOR BEGINNERS: If you are assigning the value of a word (variable) to another word (variable) in Red, COPY IT!

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

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

action! copy Red-by-example    MyCode4fun

Assigns a copy of the data to a new word.

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"]                ;b changes!!

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.


/part

Limits 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

Copies only specific types of non-scalar values.


/deep

Copies nested values, as mentioned above.



< Previous topic                                                                                          Next topic >