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 support element access via get and put

  • Both can have subsequences extracted via getinterval

  • Both can be iterated over using forall

  • Both have a length that 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

array

Create a new array of specified length

[

Begin array construction (mark)

]

End array construction

aload

Load all array elements onto stack

astore

Store stack elements into array

packedarray

Create read-only packed array (Level 2)

setpacking

Select packed or ordinary arrays for subsequently scanned procedures (Level 2)

3.2. Element Access

Command Description

get

Get single element from array/string

put

Put value into array/string element

getinterval

Extract subarray or substring

putinterval

Insert array/string into another

length

Get length of array/string

3.3. Iteration

Command Description

forall

Execute procedure for each element

4. String Categories

4.1. String Creation and Manipulation

Command Description

string

Create new string of specified length

anchorsearch

Search for prefix in string

search

Search for substring

token

Parse next token from string/file

4.2. Type Conversion

Command Description

cvs

Convert any object to string representation

cvn

Convert string to name

cvr

Convert to real number

cvi

Convert to integer

cvlit

Convert to literal attribute

cvx

Convert to executable attribute

type

Get object type as name

4.3. Access Checking

Command Description

xcheck

Check if object is executable

rcheck

Check if object is readable

wcheck

Check if object is writable

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

5.2. String Processing

Strings are commonly used for text and binary data:

(Hello, World!) length           % Get string length: 13
(PostScript) 0 4 getinterval    % Extract "Post"
(abc) (a) anchorsearch          % Test for prefix

5.3. Array/String Iteration

Process all elements in a collection:

[1 2 3 4 5] { 10 mul } forall   % Multiply each by 10: 10 20 30 40 50
(abc) { 32 add } forall         % Add 32 to each char code: 97 98 99

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]

6.3. Memory

  • Arrays allocated in VM (local or global based on allocation mode)

  • Strings allocated in VM

  • Packed arrays more memory-efficient than regular arrays

  • Subarrays/substrings share the underlying value

6.4. Type Restrictions

  • String elements must be integers 0-255 (character codes)

  • Array elements can be any PostScript object

  • Type checking errors occur on invalid operations

7. See Also