1. known

Tests whether a specific dictionary contains a given key.

1.1. Syntax

dict key known → bool

1.2. Stack Effects

Table 1. Before
Level Object

1

dict (dictionary to check)

0

key (key to search for)

Table 2. After
Level Object

0

bool (true if key exists, false otherwise)

1.3. Description

known returns true if there is an entry in dictionary dict whose key is key. Otherwise, it returns false.

The dictionary does not need to be on the dictionary stack - it can be any dictionary object.

1.4. PostScript Level

Level 1 and later

1.5. Examples

Checking for key
/mydict 5 dict def
mydict /total 0 put
mydict /total known   % Returns true
mydict /badname known % Returns false
Conditional definition
currentdict /myvar known not {
  /myvar defaultValue def
} if
Safe access
dict /key known {
  dict /key get
  % Use value
} {
  % Use default
} ifelse

1.6. Common Use Cases

1.6.1. Checking Before Access

/safeGet {  % dict key => value or null
  2 copy known {
    get
  } {
    pop pop null
  } ifelse
} def

1.6.2. Optional Parameters

/processOptions {  % dict =>
  dup /verbose known {
    /verbose get { (Verbose mode) print } if
  } if

  dup /output known {
    /output get setOutputFile
  } if

  pop
} def

1.6.3. Feature Detection

systemdict /setpagedevice known {
  % Level 2 features available
} {
  % Level 1 only
} ifelse

1.7. Common Pitfalls

Checks Specific Dictionary Only - known does NOT search the dictionary stack, only the specified dictionary.
/x 42 def  % In current dict

5 dict /x known  % Returns false (wrong dict!)
currentdict /x known  % Returns true
Different from where - known checks one dictionary; where searches the entire dictionary stack.
Use for Validation - Check before get to avoid [undefined] errors.

1.8. Error Conditions

Error Condition

[invalidaccess]

Dictionary has no-access attribute

[stackunderflow]

Fewer than 2 operands on stack

[typecheck]

First operand not a dictionary

1.9. Implementation Notes

  • Very fast operation (hash table lookup)

  • Does not modify dictionary

  • Can be used on any dictionary (not just those on dict stack)

  • Returns immediately after checking

1.10. See Also

  • where - Search dictionary stack for key

  • load - Get value from dictionary stack

  • get - Get value from specific dictionary

  • def - Define key-value pair


Back to top

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