Copy – AMT Datasouth PAL User Manual
Page 74
copy
68
Hints
The following examples demonstrate the subtle difference between using the dup and copy op-
erators. The first example shows the use of the dup operator to place a second reference to an ar-
ray onto the stack. The second example shows the use of the copy operator to make a copy of an
array into a new array. The second and third lines of each example shows the interpreter's output in
response to the == operators.
1:
[0 1 2 3] dup dup 1 (hello) put == ==
[0 (hello) 2 3]
[0 (hello) 2 3]
2:
[0 1 2 3] dup dup length array copy dup 1 (hello) put == ==
[0 (hello) 2 3]
[0 1 2 3]
As shown by the first example, the dup operator simply creates a second reference to the same
array. As a result, the put operator changes the single array referenced by both stack entries.
The second example uses the dup, length, and array operators to create a new array with the same
number of elements as the original array. The example then uses the copy operator to copy all of
the original array's contents to the new array. The put operator then only affects the new array.
Both the dup and copy operators serve seperate but equally useful functions. The dup operator
creates a second reference to an array. Since both references share the same data, both references
share any modifications made to the array. In addition, the programmer does not need to consume
additional memory by have multiple copies of the same array.
The copy operator allows the programmer to create a new array containing the same data as the
first array. This allows the programmer to modify either the first or second array without affecting
the other array. However, each array consumes memory space within the printer. In addition, any
composite objects copied from one array to the other will share their data.