Article View: comp.lang.postscript
Article #8142A "switch" procedure
From: Glenn Reid
Date: Thu, 23 Mar 1989 21:41
Date: Thu, 23 Mar 1989 21:41
60 lines
2115 bytes
2115 bytes
Several people have asked me, over the years, how to do something like
a "case" or "switch") statement in PostScript. Here is a procedure
that lets you get most of what you need out of the "switch" statement,
leaving complications like fall-through up to you. I hope somebody
finds it useful.
Glenn Reid
Adobe Systems
Developer Tools & Strategies
%!PS-Adobe-2.1
%%Title: switch.ps
%%CreationDate: Thu Mar 23 13:18:36 1989
%%Creator: Glenn Reid, Adobe Systems
%%EndComments
% "switch" behaves sort of like the C "switch" language construct.
% It is up to the programmer to build "procs" to contain the
% various procedures that should be executed and the values to trigger
% them. "fall-through" is similarly up to the programmer, as in the
% examples.
% The implementation is done with a dictionary of procedures, rather
% than attempting to use "ifelse". It can be adapted not to pass the
% dictionary if only one is needed (but loses generality and nestability).
% dictionary any "switch" -
/switch { %def
2 copy known { %ifelse
get exec
}{ %else
exch /default get exec % leave unknown val on stack for "default"
} ifelse
} bind def
/procs 12 dict begin
0 { (zero found) == } def
1 { (one found) == } def
2 { %def
(two found, and ) print
procs 3 get exec % falls through to 3
} def
3 { (three found) == } def
4 3 load def % 4 also falls through to 3
/default { %def
save exch 128 string cvs print restore
(: not found in switch statement; default used.) =
} bind def
currentdict end def
%%EndProlog
procs 0 switch
procs 1 switch
procs 2 switch
procs 3 switch
procs 4 switch
procs 5 switch
%%Trailer
Technical Details
Message-ID:
<692@adobe.UUCP>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ucbvax!decwrl!adobe!greidShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ucbvax!decwrl!adobe!greid From: greid@adobe.com (Glenn Reid) Newsgroups: comp.lang.postscript Subject: A "switch" procedure Message-ID: <692@adobe.UUCP> Date: 23 Mar 89 21:41:31 GMT Sender: news@adobe.COM Reply-To: greid@adobe.COM () Organization: Adobe Systems Incorporated, Mountain View Lines: 60