[comp.lang.postscript] Case statements in Postscript

kincaid@cg-atla.UUCP (Tom Kincaid ) (08/30/89)

If I have a postscript variable V1 that can have a value 
 1 2 3 or 4 and I want to execute 

	/proc1 if V1 = 1
	/proc2 if V1 = 2
	/proc3 if V1 = 3
	/proc4 if V1 = 4

What is the best way to write the code ? Is there a case
statement of some sort ?

montnaro@sprite.crd.ge.com (Skip Montanaro) (08/30/89)

Tom Kincaid asked about a case statement for PostScript. Sun wrote just such
a beast for NeWS. Perhaps somebody at Sun can make the decision to post it
(it is copyrighted). For anybody who might want to go out and write a case
function in PostScript, here's a syntax description of Sun's:


% A case/switch statement.  Takes a key for the case and an array.
% Generally the array is executable to avoid evaluating the array
% each time case is called:
%	obj {
%	    key1	{proc1}
%	    key2 key3	{proc2}
%	    key4	{proc3}
%	    /Default	{proc4}
%	} case


If somebody at Sun is listening, I think it would be a nice idea to post a
number of common functions defined in basics.ps, such as the rectangle and
list utilities, /case, /sprintf, and /append. I think this would help reduce
incompatible implementations of the same stuff in the future.

--
Skip Montanaro (montanaro@sprite.crd.ge.com)

bkc@image.soe.clarkson.edu (Brad Clements) (08/30/89)

From article <7573@cg-atla.UUCP>, by kincaid@cg-atla.UUCP (Tom Kincaid ):
> If I have a postscript variable V1 that can have a value 
>  1 2 3 or 4 and I want to execute 
> 
> 	/proc1 if V1 = 1
> 	/proc2 if V1 = 2
> 	/proc3 if V1 = 3
> 	/proc4 if V1 = 4
> 
> What is the best way to write the code ? Is there a case
> statement of some sort ?

This is cheap. Not in processing time though...

% define proc1 through proc4
/case_array [
	/proc1 load
	/proc2 load
	/proc3 load
	/proc4 load
] def

....
% suppose V1 now has a value between 1 and 4
	case_array v1 1 sub get exec

% presto, all done. Better off letting V1 have values 0 through 3



| Brad Clements          bkc@omnigate.clarkson.edu        bkc@clutx.bitnet 
| Network Engineer       Clarkson University              (315)268-2292
-- 

| Brad Clements          bkc@omnigate.clarkson.edu        bkc@clutx.bitnet 
| Network Engineer       Clarkson University              (315)268-2292

rainero@prism.wbst128.xerox.com (Emil Rainero) (09/01/89)

Try this



/case
{
%	(in case) ==
	/printnextproc 0 def
	/proc exch def
	/obj exch def
	{
		/proc load dup
		length 0 eq {pop exit} if
		dup 0 get 
		dup type /arraytype eq printnextproc 1 eq and {dup exec /printnextproc 2 def} if
		dup /Default eq printnextproc 0 eq and {/printnextproc 1 def} if
		/obj load eq {/printnextproc 1 def} if
		dup length 1 sub 1 exch getinterval
		/proc exch def
	} loop
} bind def


% EXAMPLE 1

0 1 20
{
	dup (     ) cvs print ( ) print
	{
		1 {(is 1) =}
		2 {(is 2) =}
		3 4 {(is 3,4) =}
		5 7 {(is 5,7) =}
		8 {(is 8) =}
		/Default {(other) =}
	} case

} for

% EXAMPLE 2
{A B C D E}
{
	dup (                         ) cvs print ( ) print
	{
			/B {(is B) =}
			/Default {(other) =}
	} case
} forall