1. where

Determines which dictionary on the dictionary stack contains a given key.

1.1. Syntax

key where → dict true (if found)
          → false (if not found)

1.2. Stack Effects

Table 1. Before
Level Object

0

key (key to search for)

Table 2. After (if found)
Level Object

1

true

0

dict (dictionary containing key)

Table 3. After (if not found)
Level Object

0

false

1.3. Description

where searches for key in each dictionary on the dictionary stack, starting with the topmost (current) dictionary.

  • If found: returns the dictionary object and true

  • If not found: returns false

This allows you to determine not just whether a key exists, but also which dictionary contains it.

1.4. PostScript Level

Level 1 and later

1.5. Examples

Finding a key
/pi 3.14159 def
/pi where {
  % Stack: dict true
  pop /pi get  % Get value: 3.14159
} {
  % Not found
  0
} ifelse
Conditional execution
/setpagedevice where {
  pop  % Level 2 available
  /A4 setpagesize
} {
  % Level 1 - use alternate method
  statusdict /a4tray known {
    statusdict begin a4tray end
  } if
} ifelse
Checking multiple dictionaries
/x 1 def  % In userdict

5 dict begin
  /x 2 def  % In local dict

  /x where {
    % Returns local dict (searched top-down)
  } if
end

1.6. Common Use Cases

1.6.1. Feature Detection

/languagelevel where {
  pop languagelevel 2 ge
} {
  false  % Level 1 (languagelevel not defined)
} ifelse

1.6.2. Safe Definition

/myvar where {
  pop  % Already defined, don't redefine
} {
  /myvar defaultValue def
} ifelse

1.6.3. Module Loading

/ModuleName where {
  pop  % Module already loaded
} {
  (module.ps) run  % Load module
} ifelse

1.7. Common Pitfalls

Returns Dictionary, Not Value - where returns the dictionary itself, not the value.
/x 42 def
/x where  % Returns: userdict true (not 42!)
True on Top - Boolean result is on top, dictionary below.
/key where {
  % Stack: dict true
  pop  % Remove true to access dict
  /key get
} if
Idiom for Getting Value - Common pattern to get value:
/key where { /key get } { defaultValue } ifelse

1.8. Error Conditions

Error Condition

[invalidaccess]

Dictionary on stack has no-access attribute

[stackoverflow]

Not enough room for results

[stackunderflow]

No operand on stack

[typecheck]

Operand wrong type

1.9. Implementation Notes

  • Searches dictionary stack top-down

  • Stops at first match

  • Returns actual dictionary object

  • Fast operation (series of hash lookups)

1.10. Comparison with known

Aspect known where

Search scope

Specific dictionary

Entire dictionary stack

Returns

Boolean only

Dictionary + boolean

Syntax

dict key known

key where

Not found

false

false

1.11. See Also

  • known - Check specific dictionary

  • load - Get value from dictionary stack

  • def - Define in current dictionary

  • store - Store in dictionary stack

  • get - Get from specific dictionary


Back to top

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