1. get
Retrieves a single element from an array, packed array, dictionary, or string.
1.1. Syntax
array index get → any packedarray index get → any dict key get → any string index get → int
1.2. Stack Effects
| Level | Object |
|---|---|
1 |
|
0 |
|
| Level | Object |
|---|---|
0 |
Element at |
| Level | Object |
|---|---|
1 |
|
0 |
|
| Level | Object |
|---|---|
0 |
Value associated with |
1.3. Description
get retrieves a single element from a composite object:
-
For arrays and packed arrays: returns the element at the specified index
-
For strings: returns the integer character code (0-255) at the specified index
-
For dictionaries: returns the value associated with the specified key
Indices for arrays and strings are 0-based: valid range is 0 to length-1.
1.5. Examples
[31 41 59] 0 get % Returns 31
[31 41 59] 2 get % Returns 59
[0 (a mixed array) [ ] { add }] 1 get
% Returns (a mixed array)
(abc) 1 get % Returns 98 (ASCII 'b')
(a) 0 get % Returns 97 (ASCII 'a')
/mydict 5 dict def
mydict /mykey (myvalue) put
mydict /mykey get % Returns (myvalue)
1.6. Common Use Cases
1.7. Common Pitfalls
String Returns Integer - get on a string returns an integer (character code), not a one-character string.
|
(abc) 0 get % Returns 97, not (a)
Index Range - Index must be in range 0 to length-1. Out-of-bounds access causes [rangecheck] error.
|
[1 2 3] 3 get % Error: valid indices are 0-2
Dictionary Key Not Found - If the key doesn’t exist in a dictionary, [undefined] error occurs.
|
5 dict /nokey get % Error: key not in dictionary
Use known/where First - For dictionaries, use where or known to check if a key exists before using get.
|
1.8. Error Conditions
| Error | Condition |
|---|---|
[ |
Object has no-access or execute-only attribute |
[ |
Index out of bounds for array/string |
[ |
Fewer than 2 operands on stack |
[ |
First operand wrong type, or index not integer |
[ |
Key not found in dictionary |
1.9. Implementation Notes
-
For arrays and dictionaries, returns the actual object (not a copy)
-
For strings, returns a new integer object
-
Accessing composite elements creates no new objects (shares references)
-
Dictionary lookup is typically O(1) average case
1.11. See Also
-
put- Store value in array/string/dictionary -
getinterval- Get subarray or substring -
length- Get length of array/string -
aload- Load all array elements -
forall- Iterate over elements