1. store
Stores a value in the dictionary stack (searches for existing key first).
1.2. Stack Effects
| Level | Object |
|---|---|
1 |
|
0 |
|
| Level | Object |
|---|---|
(empty) |
Dictionary modified |
1.3. Description
store searches for key in each dictionary on the dictionary stack, starting with the topmost (current) dictionary.
-
If found: replaces the value in that dictionary
-
If not found: creates new entry in current dictionary
This differs from def, which always uses the current dictionary.
1.5. Examples
/x 1 def % Define in current dict
5 dict begin
/x 2 def % New x in new dict
/x 3 store % Modifies x in current dict (now 3)
end
/x load % Returns 1 (outer x unchanged)
/counter 0 def
% ... later ...
/counter counter 1 add store % Increment counter
/newvar 42 store % Same as /newvar 42 def (not found)
1.6. Common Use Cases
1.7. Common Pitfalls
Searches Dictionary Stack - store may modify a dictionary other than the current one!
|
/x 1 def % In userdict
10 dict begin
/x 99 store % Modifies x in userdict, not current dict!
end
Global/Local VM Restrictions - Same VM rules as def apply.
|
Use def for New Definitions - Use def when you want to ensure definition goes in current dictionary.
|
Use store for Updates - Use store when you want to update an existing definition regardless of where it is.
|
1.8. Error Conditions
| Error | Condition |
|---|---|
[ |
Current dictionary full and key not found (Level 1) |
[ |
Global VM dict would receive local VM value |
[ |
Dictionary expansion exceeds limits |
[ |
Fewer than 2 operands on stack |
1.9. Implementation Notes
-
Equivalent to:
key where { exch put } { currentdict exch put } ifelse -
May trigger dictionary expansion in Level 2
-
Searches top-down through dictionary stack
-
Creates entry in current dict only if key not found