Table of Contents
1. where
Determines which dictionary on the dictionary stack contains a given key.
1.2. Stack Effects
| Level | Object |
|---|---|
0 |
|
| Level | Object |
|---|---|
1 |
|
0 |
|
| Level | Object |
|---|---|
0 |
|
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.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.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 |
|---|---|
[ |
Dictionary on stack has no-access attribute |
[ |
Not enough room for results |
[ |
No operand on stack |
[ |
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)