1. putinterval
Replaces a subsequence of an array or string with the contents of another array or string.
1.1. Syntax
array1 index array2 putinterval → – array1 index packedarray2 putinterval → – string1 index string2 putinterval → –
1.2. Stack Effects
| Level | Object |
|---|---|
2 |
|
1 |
|
0 |
|
| Level | Object |
|---|---|
(empty) |
Destination modified in place |
1.3. Description
putinterval replaces a subsequence of the first operand starting at index with the entire contents of the third operand.
The objects are copied from the source to the destination as if by a sequence of individual get and put operations. For arrays, if copied elements are composite objects, their values are shared between source and destination.
Requirements: * index must be valid (0 to length-1) * index + length(source) must not exceed length(destination)
1.5. Examples
/ar [5 8 2 7 3] def
ar 1 [(a) (b) (c)] putinterval
ar % Now [5 (a) (b) (c) 3]
/st (abc) def
st 1 (de) putinterval
st % Now (ade)
/src [10 20 30] def
/dest [0 0 0 0 0] def
dest 1 src putinterval
dest % Now [0 10 20 30 0]
1.6. Common Use Cases
1.6.1. String Construction
/buffer 20 string def
buffer 0 (Hello, ) putinterval
buffer 7 (World!) putinterval
buffer 0 13 getinterval % "Hello, World!"
1.7. Common Pitfalls
| Destination Must Be Large Enough - index + length(source) must not exceed destination length. |
[1 2 3] 2 [4 5] putinterval % Error: 2+2 > 3
| Cannot Modify Packed Arrays - Packed arrays are read-only. |
<array> 0 [1 2] putinterval % Error if <array> is packed
| Shared Composite Objects - When copying arrays containing composite objects, the values are shared. |
/src [[1]] def
/dest [null] def
dest 0 src putinterval
dest 0 get 0 99 put % Modifies BOTH arrays!
VM Restrictions - If destination is in global VM and source contains local VM objects, [invalidaccess] error occurs.
|
Efficient Copying - putinterval is more efficient than multiple put operations for bulk copying.
|
1.8. Error Conditions
| Error | Condition |
|---|---|
[ |
Destination in global VM contains local VM source, or destination is read-only |
[ |
index + source length > destination length |
[ |
Fewer than 3 operands on stack |
[ |
Wrong operand types or type mismatch |
1.9. Implementation Notes
-
Elements are copied, not moved
-
For composite array elements, references are copied (values shared)
-
Packed arrays can be source but not destination
-
String elements are copied by value (integers 0-255)
1.10. Performance Considerations
-
Much faster than individual
putoperations -
Efficient for bulk array/string operations
-
No intermediate objects created
-
Memory bandwidth limited for large copies
1.11. See Also
-
getinterval- Extract subarray/substring -
put- Store single element -
get- Get single element -
astore- Store stack into array