Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postscript: concatenate two strings?

Tags:

postscript

How do I concatenate two strings in Postscript?

(foo) (bar) ??? -> (foobar)
like image 203
Mark Harrison Avatar asked Sep 11 '12 22:09

Mark Harrison


People also ask

How do I concatenate a string in a PowerShell script?

In PowerShell, string concatenation is primarily achieved by using the “+” operator. There are also other ways like enclosing the strings inside double quotes, using a join operator, or using the -f operator. $str1="My name is vignesh."

How do you concatenate in a script?

String concatenation is the process of appending a string to the end of another string. This can be done with shell scripting using two methods: using the += operator, or simply writing strings one after the other.

How do I add a variable to a string in PowerShell?

PowerShell Variable ExamplesYou can create a variable by simply assigning it a value. For example, the command $var4 = “variableexample” creates a variable named $var4 and assigns it a string value. The double quotes (” “) indicate that a string value is being assigned to the variable.


3 Answers

PostScript doesn't have a built-in string concatenation operator. You need to write some code for that. For instance

 /concatstrings % (a) (b) -> (ab)  
   { exch dup length    
     2 index length add string    
     dup dup 4 2 roll copy length
     4 -1 roll putinterval
   } bind def  

(code from https://en.wikibooks.org/wiki/PostScript_FAQ/Programming_PostScript#How_to_concatenate_strings%3F.)

like image 193
lhf Avatar answered Sep 28 '22 06:09

lhf


Same idea generalized to any number of strings. Earlier revisions use a helper function acat which takes an array of strings (for easy counting and iteration). This version uses fancier loops and stack manipulation to avoid allocating an array. This version would also concatenate arrays by changing the string operator to array.

% (s1) (s2) (s3) ... (sN) n  ncat  (s1s2s3...sN)
/ncat {        % s1 s2 s3 .. sN n                   % first sum the lengths
    dup 1 add  % s1 s2 s3 .. sN n n+1 
    copy       % s1 s2 s3 .. sN n  s1 s2 s3 .. sN n
    0 exch     % s1 s2 s3 .. sN n  s1 s2 s3 .. sN 0 n 
    {   
        exch length add 
    } repeat             % s1 s2 s3 .. sN  n   len  % then allocate string
    string exch          % s1 s2 s3 .. sN str   n   
    0 exch               % s1 s2 s3 .. sN str  off  n
    -1 1 {               % s1 s2 s3 .. sN str  off  n  % copy each string
        2 add -1 roll       % s2 s3 .. sN str  off s1  % bottom to top
        3 copy putinterval  % s2 s3 .. sN str' off s1
        length add          % s2 s3 .. sN str' off+len(s1)
                            % s2 s3 .. sN str' off'
    } for                               % str' off'
    pop  % str'
} def 

(abc) (def) (ghi) (jkl) 4 ncat == %(abcdefghijkl)
like image 29
luser droog Avatar answered Sep 28 '22 05:09

luser droog


There are useful subroutines in

http://www.jdawiseman.com/papers/placemat/placemat.ps

including Concatenate (accepting two strings) and ConcatenateToMark (mark string0 string1 …).

like image 41
jdaw1 Avatar answered Sep 28 '22 04:09

jdaw1