🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.postscript
Article #1277

Re: Novice Question

#1277
From: Don Hopkins
Date: Mon, 30 Oct 1989 10:08
56 lines
2631 bytes
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
Technical Details
Message-ID: <20449@mimsy.umd.edu>
Path: arc1-ca.pugleaf.net!utzoo!attcan!utgpu!jarvis.csri.toronto.edu!rutgers!usc!brutus.cs.uiuc.edu!samsung!aplcen!haven!mimsy!brillig.umd.edu!don
References: <7834@cg-atla.UUCP> <2884@eagle.wesleyan.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!utgpu!jarvis.csri.toronto.edu!rutgers!usc!brutus.cs.uiuc.edu!samsung!aplcen!haven!mimsy!brillig.umd.edu!don
From: don@brillig.umd.edu (Don Hopkins)
Newsgroups: comp.lang.postscript
Subject: Re: Novice Question
Message-ID: <20449@mimsy.umd.edu>
Date: 30 Oct 89 10:08:36 GMT
References: <7834@cg-atla.UUCP> <2884@eagle.wesleyan.edu>
Sender: news@mimsy.umd.edu
Reply-To: don@brillig.umd.edu.UUCP (Don Hopkins)
Distribution: comp
Organization: U of Maryland, Dept. of Computer Science, Human Computer Interaction Lab
Lines: 56