1. put
Stores a value into an array, dictionary, or string element.
1.2. Stack Effects
| Level | Object |
|---|---|
2 |
|
1 |
|
0 |
|
| Level | Object |
|---|---|
2 |
|
1 |
|
0 |
|
| Level | Object |
|---|---|
(empty) |
Composite object modified in place |
1.3. Description
put replaces a single element in a composite object:
-
For arrays: stores any at position index (0 to n-1)
-
For strings: stores integer int (0-255) at position index
-
For dictionaries: associates any with key (creates new entry if key doesn’t exist)
The composite object is modified in place. If the array or dictionary value is in global VM and any is in local VM, an [invalidaccess] error occurs.
1.5. Examples
/ar [5 17 3 8] def
ar 2 (abcd) put
ar % Now [5 17 (abcd) 8]
/d 5 dict def
d /abc 123 put
d { } forall % Shows /abc 123
/st (abc) def
st 0 65 put % 65 is ASCII 'A'
st % Now (Abc)
/point [100 200] def
point 0 150 put % Change x to 150
point % Now [150 200]
1.6. Common Use Cases
1.6.1. Array Element Updates
/colors [/red /green /blue] def
colors 1 /yellow put % Replace green with yellow
1.7. Common Pitfalls
| String Values Must Be Integers - Strings store character codes (0-255), not character strings. |
(abc) 0 (X) put % Error: must be integer
(abc) 0 88 put % Correct: 88 is ASCII 'X'
| Index Range - Array/string index must be 0 to length-1. |
[1 2 3] 3 99 put % Error: index 3 out of range
Packed Arrays are Read-Only - Cannot use put on packed arrays.
|
currentpacking true setpacking
{ 1 2 3 } 0 99 put % Error: packed array
Dictionary Size (Level 1) - In Level 1, if dictionary is full, [dictfull] error occurs.
|
| Dictionary Auto-Expands (Level 2) - In Level 2, dictionaries automatically expand when needed. |
1.8. Error Conditions
| Error | Condition |
|---|---|
[ |
Dictionary full (Level 1 only) |
[ |
Object in global VM contains local VM value, or object is read-only |
[ |
Index out of bounds, or int not in 0-255 for strings |
[ |
Fewer than 3 operands on stack |
[ |
Wrong operand types |
1.9. Implementation Notes
-
Modifies the object in place (does not create a copy)
-
For composite values (arrays, dicts), stores by reference
-
Dictionary lookup to check for existing key
-
In Level 2, dictionaries grow automatically when needed
1.10. Performance Considerations
-
Array and string
putare very fast (direct indexing) -
Dictionary
putslightly slower (hash table operation) -
Repeated dictionary puts in Level 1 may trigger slow rehashing if dictionary grows full
-
For bulk updates, consider
putinterval
1.11. See Also
-
get- Get element from array/string/dictionary -
putinterval- Put subarray/substring -
astore- Store multiple elements into array -
length- Get length of array/string