To create an array in PostScript, you use square brackets [ ]
. Each item within the brackets becomes an element of the array. Here’s how you create a simple array:
[1 2 3 4 5] % Creates an array of five integers
Arrays can also be mixed-type:
[100 (string) /name] % An array containing an integer, a string, and a name
Elements in a PostScript array are accessed by an index. PostScript arrays are zero-indexed, which means the first element is accessed with index 0
. You use the get
operator to retrieve an element:
/myArray [10 20 30 40 50] def
myArray 2 get % Gets the third element, which is 30
To modify an element of an array, you use the put
operator, specifying the array, the index, and the new value:
myArray 1 25 put % Sets the second element of myArray to 25
Arrays in PostScript can be manipulated with various operators:
- length: Returns the number of elements in the array.
myArray length % Returns 5
- copy: Copies elements from one array to another or duplicates the entire array.
/newArray 5 array def myArray newArray copy % Copies myArray to newArray
- forall: Iterates over each element of the array, applying a procedure.
myArray {100 mul} forall % Multiplies each element by 100
- index: Accesses an element by its index without removing it from the stack.
myArray 3 index % Gets the fourth element without altering the stack
- aload and astore:
aload
copies all elements of an array to the stack, andastore
does the reverse, storing elements from the stack into an array.myArray aload % Pushes all elements of myArray onto the stack 5 array astore % Stores top five stack items into a new array
Arrays can contain other arrays, allowing the creation of multi-dimensional data structures:
[ [1 2 3] [4 5 6] [7 8 9] ] % A 3x3 matrix represented as an array of arrays
Arrays are particularly useful in PostScript for defining paths and patterns, storing transformation matrices, and managing graphical states. For example, transformation matrices are typically represented as arrays:
[1 0 0 1 30 50] concat % Applies a translation transform using an array