[comp.lang.postscript] Novice Question

kincaid@cg-atla.UUCP (Tom Kincaid ) (10/28/89)

Problem:	I am exporting postscript to another program. My code
		evaluates a Postscript variable "V1" that under
		certain conditions is defined by the importing program
		and under other conditions is not.

		So sometimes I get the error:
		
		" V1 undefined " 

		from the interpreter.


Is there a way to:  

		-Check to see if V1 is defined 

		-if it is defined, do nothing.

		-if it is not defined, then define it.


		Thanks,
				Tom

rsilverman@eagle.wesleyan.edu (10/28/89)

In article <7834@cg-atla.UUCP>, kincaid@cg-atla.UUCP (Tom Kincaid ) writes:

> Is there a way to:  
> 
> 		-Check to see if V1 is defined 
> 
> 		-if it is defined, do nothing.
> 
> 		-if it is not defined, then define it.
> 
> 
> 		Thanks,
> 				Tom

Tom,
	The "known" operator tries to look up its operand in the context of the
dictionary stack, and returns true or false accordingly.  Thus:

/V1 known not { /V1 null def } if

will do what you want (replace "null" with whatever you want).

						Richard Silverman

don@brillig.umd.edu (Don Hopkins) (10/30/89)

In article <2884@eagle.wesleyan.edu> rsilverman@eagle.wesleyan.edu writes:
>In article <7834@cg-atla.UUCP>, kincaid@cg-atla.UUCP (Tom Kincaid ) writes:
>
>> Is there a way to:  
>> 
>> 		-Check to see if V1 is defined 
>> 		-if it is defined, do nothing.
>> 		-if it is not defined, then define it.
>> 
>> 		Thanks,
>> 				Tom
>
>Tom,
>	The "known" operator tries to look up its operand in the context of the
>dictionary stack, and returns true or false accordingly.  Thus:
>
>/V1 known not { /V1 null def } if
>
>will do what you want (replace "null" with whatever you want).
>
>						Richard Silverman

Sorry, this won't work. 'known' takes a dict and a key as arguments,
and returns a boolean. You're not supplying it with a dict.  What's
better to use is 'where', which just takes a key, and returns either a
boolean false, or the first dictionary on the dictstack in which the
key was found and a boolean true.  'known' does not search the
dictionary stack, it only looks in the dictionary you pass it.
'where' is better for what you are doing than 'known' because it will
find definitions that are in systemdict, userdict, or any other
dictionary on the dict stack. (The recent header file that was posted
to comp.lang.postscript, which looked for /letter only in userdict,
should probably be using 'where', so it does the right thing in cases
where /letter is defined in systemdict.)

/known 	% dict key => true/false

/where  % key => false			(if key not defined on dict stack)
	% key => dict true		(if key defined on dict stack)

Note that 'where' leaves a different number of things on the stack 
depending on if the key was found or not. A where is usually followed
by an if or an ifelse statement whose true clause consumes the dict, 
so the same number of items are left on the stack in either case.

i.e.:

/foobar where {
  pop % throw away the dict foobar was found in
  (foobar was already defined, as ) print foobar =
} {
  /foobar 123 def
  (foobar is now defined as ) print foobar =
} ifelse

	-Don