1. length

Returns the number of elements in an array, packed array, string, dictionary, or name.

1.1. Syntax

array length → int
packedarray length → int
dict length → int
string length → int
name length → int

1.2. Stack Effects

Table 1. Before
Level Object

0

Composite object or name

Table 2. After
Level Object

0

int (number of elements)

1.3. Description

length returns the length of its operand:

  • For arrays and packed arrays: number of elements

  • For strings: number of characters (bytes)

  • For dictionaries: current number of key-value pairs (not maximum capacity)

  • For names: number of characters in the name

For dictionaries, use maxlength to get the maximum capacity.

1.4. PostScript Level

Level 1 and later

1.5. Examples

Array length
[1 2 4] length          % Returns 3
[] length               % Returns 0
String length
(abc) length            % Returns 3
(hello world) length    % Returns 11 (space counts)
() length               % Returns 0
Dictionary length
/mydict 5 dict def
mydict length           % Returns 0 (empty)
mydict /key1 (value1) put
mydict length           % Returns 1
Name length
/foo length             % Returns 3
/x length               % Returns 1

1.6. Common Use Cases

1.6.1. Loop Bounds

/arr [10 20 30 40] def
0 1 arr length 1 sub {
  arr exch get
  % Process element
} for

1.6.2. Dynamic Array Processing

/data [...] def
data length array  % Create array of same size

1.6.3. String Buffer Allocation

/text (Original text) def
text length string  % Allocate string of same length

1.7. Common Pitfalls

Dictionary Length vs. Capacity - length returns the current number of entries, not the maximum capacity.
5 dict length         % Returns 0, not 5
5 dict maxlength      % Returns 5 (capacity)
Name vs. String - Name length counts characters in the name, not the string representation.
/abc length           % Returns 3
(abc) length          % Also returns 3
Use with forall - For iterating over all elements, forall is often more convenient than using length with a loop.

1.8. Error Conditions

Error Condition

[invalidaccess]

Object has no-access attribute

[stackunderflow]

No operand on stack

[typecheck]

Operand is not array, packed array, dict, string, or name

1.9. Implementation Notes

  • Very fast operation (O(1) - length is stored with the object)

  • For arrays and strings, length is fixed at creation time

  • For dictionaries, length changes as entries are added/removed

  • Packed arrays have fixed length like regular arrays

Table 3. Comparison of length-related operators
Operator Returns

length

Current number of elements/entries

maxlength

Maximum capacity (dictionaries only)

count

Number of objects on operand stack

1.11. See Also

  • get - Get element at index

  • getinterval - Get subarray/substring

  • forall - Iterate over all elements

  • array - Create array with specific length

  • string - Create string with specific length


Back to top

Copyright © 2025 Ribose. PostScript is a trademark of Adobe. Distributed under the MIT License.