Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split a string into array in PostScript

Tags:

postscript

What is the easiest way to split a string into array by a given character? For example, making an array of words by splitting over space; or even making an array of all characters of the string.

The only method I can think of is to use search in a loop. Since, all languages have a function for this purpose, I am afraid that I'm missing a function in PostScript to do so.

like image 926
Googlebot Avatar asked Sep 07 '12 21:09

Googlebot


2 Answers

%!

%(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)]
like image 189
luser droog Avatar answered Oct 28 '22 22:10

luser droog


You're on the right track with the search operator. It's purpose is to perform textual string searching and matching. Here is the search operator summary found in the PostScript Language Reference Manual:

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  
like image 29
Dave M Avatar answered Oct 28 '22 21:10

Dave M