1. Array and String Operations
Array and string operations provide the foundation for data structure manipulation in PostScript. These commands enable creating, accessing, modifying, and iterating over arrays and strings, which are the primary composite data types in PostScript.
2. Overview
Arrays and strings in PostScript share many operational characteristics:
-
Both are indexed collections (0-based indexing)
-
Both can have subsequences extracted via
getinterval -
Both can be iterated over using
forall -
Both have a
lengththat can be queried
However, they differ in important ways:
-
Arrays contain arbitrary PostScript objects; strings contain only integers (0-255)
-
Arrays can be literal or executable; strings are always literal unless explicitly converted
-
Strings are mutable; packed arrays are read-only
3. Array Categories
3.1. Array Creation and Manipulation
| Command | Description |
|---|---|
Create a new array of specified length |
|
Begin array construction (mark) |
|
End array construction |
|
Load all array elements onto stack |
|
Store stack elements into array |
|
Create read-only packed array (Level 2) |
|
Select packed or ordinary arrays for subsequently scanned procedures (Level 2) |
3.2. Element Access
| Command | Description |
|---|---|
Get single element from array/string |
|
Put value into array/string element |
|
Extract subarray or substring |
|
Insert array/string into another |
|
Get length of array/string |
4. String Categories
5. Common Patterns
5.1. Array Construction
The most common way to create arrays uses the bracket syntax:
[ 1 2 3 ] % Creates [1 2 3]
[ (hello) /world ] % Mixed types
For dynamic array creation:
3 array % Create array of length 3: [null null null]
dup 0 42 put % Set first element to 42
6. Important Considerations
6.1. Indexing
-
Arrays and strings use 0-based indexing
-
Valid indices range from 0 to length-1
-
Out-of-bounds access generates [
rangecheck] error
6.2. Mutability
-
Regular arrays are mutable (can be modified)
-
Packed arrays (Level 2) are read-only for efficiency
-
Strings are mutable (unlike in many other languages)
-
Access attributes can restrict modification via [
readonly]
7. See Also
-
Stack Manipulation Commands - For basic data movement
-
Arithmetic and Math Commands - For numeric operations
-
Control Flow Commands - For conditional and loop constructs (to be documented)