I occasionally dabble with postscript, it is a quirky language I like, but I am far from expert.
To produce diagrams like this:
I have written some code that looks like this:
% fontsize on stack
label (Up) eq
{ /Symbol exch selectfont (\255) show }
{ label (Down) eq
{ /Symbol exch selectfont (\257) show }
{ label (Left) eq
{ /Symbol exch selectfont (\254) show }
{ label (Right) eq
{ /Symbol exch selectfont (\256) show }
{ label (bs) eq
{ /Symbol exch selectfont (\254) show }
{ /Helvetica exch selectfont
label length 3 lt
{ label show }
{
label (/) % (page/up) (/)
search % (up) (/) (page) true
{ % (up) (/) (page)
/Helvetica keyHeight 4.6 div selectfont
3 1 roll % (page) (up) (/)
pop % (page) (up)
currentpoint % (page) (up) 438 745
3 2 roll % (page) 438 745 (up)
show % (page) 438 745
moveto % (page)
0 keyHeight 0.25 mul % (page) 0 11
rmoveto % (page)
show } %
{ show } ifelse
} ifelse
} ifelse
} ifelse
} ifelse
} ifelse
} ifelse
...
(End)key (Page/Down)key 0.5 gap (7)(Home)key2 (8)(Up)key2 ...
I don't like the nested if statements and repetition but it was a first quick stab at a solution. My first thought was I'd use a case statement in another language. For Postscript I guess I should choose a suitable data structure for the pairs of label names and corresponding octal character codes. Perhaps a nested array.
[ [(Up) (\255)] [(Down) (\257)] ... ]
But I'm unsure how I could iterate over this and still elegantly keep track of whether I need to execute a default procedure when no names matched.
Is there a good idiom I could follow?
OK now I'm back at my desk. Rather than answer this myself let me point you at an excellent resource instead, John Deubert's Acumen Training Journal:
If you look at the April 2001 PostScript tech journal you'll see John's clear description of how to implement the case statement using dictionaries. I'd suggest this is better than using arrays for two main reasons, firstly the ability to simply add a default case, and secondly the fact that the 'get' and 'known' operators are, as John points out, usually highly optimised in PostScript interpreters, as they are used very frequently.