Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!mailrus!ames!decwrl!adobe!ondine!greid
From: greid@ondine.COM (Glenn Reid)
Newsgroups: comp.lang.postscript
Subject: Re: string concatenation
Summary: dup length string putinterval etc
Keywords: PostScript strings
Message-ID: <4059@adobe.COM>
Date: 4 Jul 88 02:37:37 GMT
References: <2564@ihuxy.ATT.COM>
Sender: news@adobe.COM
Reply-To: greid@ondine.UUCP (Glenn Reid)
Organization: Adobe Systems Incorporated, Mountain View
Lines: 60

>I'm learning PS slowly and have a couple questions.  I have all three
>Adobe books and have tried to infer one thing in particular to no avail.
>How do you concatenate several strings together into one in PS?  A small
>example would be worth a thousand words.  I have tried "copy" and "put"
>but my programming environment is becoming a real source of frustration.
 
Jerry,
 
There is no inherent mechanism for string concatenation in the
PostScript language.  Basically you have to allocate a new string
and copy the existing ones into it.  You might also pause to consider
carefully why you want to concatenate the strings: you might not need
to (for example, if you are going to print them sequentially, all you
have to do is call "show" for each string--the current point is left at
the right spot after each show, and the next string is printed right
where you would expect).

Anyway, here is a procedure that should help (be aware that it uses
memory each time it is called, equal to the sum of the lengths of the
strings you are concatenating):

%!PS-Adobe-2.0
%%Title: stringcat.ps
%%EndComments
/cat { %def
  % concatenates two strings and leaves the result on the stack
    dup length 2 index length add string% new string
    dup 4 -1 roll			% pull (one) to top
    dup length 3 1 roll			% save  for later
    0 exch putinterval			% () 0 (one) putinterval
    1 index exch			% keep one last copy of ()
    4 -1 roll putinterval		% ()  ( two) putinterval
					% leaves last copy of () on stack
} bind def

/scratch 3 dict def
/cat2 { %def
   % uses variables instead of the stack; take your pick
    scratch begin
	/str2 exch def /str1 exch def	% save from stack
	/resultstr			% compute length, allocate string
	    str1 length
	    str2 length add string
	def
	resultstr 0 str1 putinterval	% putinterval str1 at 0
	resultstr str1 length str2
			putinterval	% putinterval str2 at length(str1)
	resultstr			% leave result string on stack
    end
} bind def
%%EndProlog

(one) ( two) cat  ==
(first ) (second) cat2  dup ==

%%Trailer
% I hope this helps.
%
% Glenn Reid
% Adobe Systems