I am using Ghostscript under Linux, and still feel like a newbie. I want to save the value on top of the stack so that I can recall the value later without having to do complex stack operations to retrieve it. The value will be a latitude read from a data file so I cannot incorporate it direct;y in the program. The obvious solution seems to me to store the value as a key/value pair in the userdict, something like (to save a latitude value):
2 dict coords
coords lat [some code to get the value of the latitude on the stack] put
Please can someone explain how to do the bit in and including the brackets? It may well be that I have it all wrong, in which case please, what do I do instead?
Background: I am writing a program to draw the gores that can be cut out and pasted on to a ball to make a world globe, using latitude and longitude data from a file. There is a fair bit of maths needed to scale the longitudes to fit them correct;y in the gores.The data I am using is the set of 1320 coordinates in world.dat
included with the gnuplot installation.
You haven't really given enough to go on, what are 'coords' and 'lat' ?
You've created a 2 entry dictionary on the operand stack, is 'coords' an executable procedure which fills that in ? If so does it return the dictionary on the operand stack ?
By executing 'put' the way you have, you will define an array in the current dictionary, with a key of 'lat' whatever object type that is. You would then need access to 'lat' again in order to retrieve the object from the dictionary (or use forall on the dictionary, and it would still be hard to retrieve the particular value).
To answer your question, this will define the value on the top of the operand stack in the current dictionary:
5 %% put an object on top of the dictionary stack, stack is now: 5
/Saved %% create a name object on the operand stack, stack is now: 5 /Saved
exch %% exchange the objects, stack is now: /Saved 5
def %% define the value on the stack in the current dictionary, using the
%% key also on the stack
Note that this is defied in the current dictionary, the one on top of the dictionary stack. To explicitly define it in userdict, do
5 %% put an object on the operand stack, stack is now: 5
/Saved %% put a name object on the operand stack, stack is now: 5 /Saved
exch %% exchange the objects, stack is now: /Saved 5
userdict %% put a pointer to userdict on the operand stack, stack is now /Saved 5 <<userdict>>
3 1 roll %% roll the stack, stack is now: <<userdict>> /Saved 5
put %% put the value from the operand stack, using the key from the operand stack, in the dictionary also on the operand stack