2012-09-07 3 views
5

Was ist der einfachste Weg, einen String durch ein gegebenes Zeichen in ein Array aufzuteilen? Zum Beispiel, ein Array von Wörtern durch Teilen über den Raum zu machen; oder sogar ein Array aller Zeichen der Zeichenfolge erstellen.Wie teilt man einen String in ein Array in PostScript auf?

Die einzige Methode, die ich mir vorstellen kann, ist search in einer Schleife zu verwenden. Da zu diesem Zweck alle Sprachen eine Funktion haben, fürchte ich, dass mir dazu eine Funktion in PostScript fehlt.

+1

Ich habe ein Beispiel für die Verwendung Suche in einer Schleife in meiner Antwort hier: http://stackoverflow.com/a/5846955/733077 –

Antwort

6

Sie sind mit dem Operator search auf dem richtigen Weg. Sein Zweck besteht darin, Textstringsuchen und -abgleich durchzuführen. Hier ist die search Operator Zusammenfassung in den PostScript Language Reference Manual gefunden:

search string seek search post match pre true (if found) 
     string false (if not found) 

     looks for the first occurrence of the string seek within string and 
     returns results of this search on the operand stack. The topmost 
     result is a boolean that indicates if the search succeeded. 

     If search finds a subsequence of string whose elements are equal 
     to the elements of seek, it splits string into three segments: 
     pre, the portion of string preceding the match; match, the portion 
     of string that matches seek; and post, the remainder of string. It 
     then pushes the string objects post, match, and pre on the operand 
     stack, followed by the boolean true. All three of these strings are 
     substrings sharing intervals of the value of the original string. 

     If search does not find a match, it pushes the original string 
     and the boolean false. 

     Example: 

      (abbc) (ab) search ==> (bc) (ab) () true 
      (abbc) (bb) search ==> (c) (bb) (a) true 
      (abbc) (bc) search ==>() (bc) (ab) true 
      (abbc) (B) search ==> (abbc) false 
5
%! 

%(string) (delimiter) split [(s)(t)(r)(i)(n)(g)] 
/split {    % str del 
    [ 3 1 roll  % [ str del 
    {     % [ ... str del 
     search {  % [ ... post match pre 
      3 1 roll % [ ... pre post match %ie. [ ... pre str' del 
     }{   % [ ... str 
      exit  % [ ... str %% break-from-loop 
     }ifelse 
    }loop    % [ ... 
    ]     % [ ... ] 
} def 

(string of words separated by spaces)()split == 
%-> [(string) (of) (words) (separated) (by) (spaces)] 

(string.of.words.separated.by.dots)(.)split == 
%-> [(string) (of) (words) (separated) (by) (dots)]