2016-08-23 5 views
2

が、私は以前、私は、彼らが等しいかどうかを確認し、それらがある場合は、何らかのアクションを実行する必要がのPostScript - 構造

/smth1 [0 1 0] def 
/smth2 [-1 0 0] def 

...

を定義している「場合は」平等+の値を比較すると、例:(Equal!) show

私は、おそらくeqとを、使用

... {(Equal!) show} if 

のようなものしかし、私は正しく以前smth1smth2定義を比較する方法を見つけ出すことはできません必要があることを知っています。

お知らせください。

答えて

4

配列を比較したくない場合は、配列の内容を比較する必要があります。配列やその他の複合オブジェクトは、PostScriptで同等かどうかをテストできますが、その内容が同じオブジェクトであるかどうかだけをテストするわけではありません。

例:

%! 
/Array1 [0 0 0] def 
/Array2 [0 0 0] def 
/Pointer Array1 def 

Array1 Array2 eq 
{ 
    (Array1 equals Array2\n) print 
} 
{ 
    (Array1 does not equal Array2\n) print 
} 
ifelse 

Array1 Pointer eq 
{ 
    (Array1 equals Pointer\n) print 
} 
{ 
    (Array1 does not equal Pointer\n) print 
} 
ifelse 

あなたがいることを実行する場合、あなたは配列1と配列2が等しくないことがわかりますが、配列1とポインタがあることでしょう。これは、ポインタが(疎に)Array1へのポインタであるためです。実際、PostScriptが動作する方法は、どちらも同じオブジェクトへの参照です。 Array1とArray2は、内容が同じであっても、異なるオブジェクトへの参照です。

あなたの場合、配列の各要素を取得し、別の配列の同じ要素と比較したいとします。両者が等しくない場合は中止し、そうでない場合は続行します。私たちが使用する

便利な演算子:長さ、EQ、取得、ため、DUP、EXCH、

ifelse以下の例は、作業溶液を示すものではありませんが、あなたがこの問題に対処する方法を与える必要があり、場合:

例1、今コンテンツを

%! 
%% First let us define two arrays with the same contents 

userdict begin  %% We'll define these in user dict 
/Array1 [0 0 0] def 
/Array2 [0 0 0] def 


Array1 length Array2 length eq 
{ 
    % The 'for' operator consumes 4 operands, the initial value of the loop counter, 
    % the amount to increment the counter by on each pass, and the terminating 
    % value of the counter, finally the executable array to execute on each pass. 
    % So, starting at loop count = 0, incrementing by 1 each time, and stopping 
    % when the counter is the length of the array. Note! Because we start at 0 
    % The counter is the array length - 1. 
    0 1 Array1 length 1 sub 
    { 
    %% Now on each pass the top element on the stack is the loop counter 
    %% We're going to need that twice, once for each array. So we start by 
    %% taking a copy and putting it on the stack 
    dup 
    %% The stack now contains: <loop count> <loop count> 
    %% Now get the n'th element from the first array: 
    get 
    %% The stack now contains: <loop count> <array1 element 'n'> 
    %% We want to use the loop counter to index the second array, but its not 
    %% on top of the stack, so swap the top two elements: 
    exch 
    %% Stack now contains: <array1 element 'n'> <loop count> 
    %% Now use the counter to get the n'th element from the second array 
    get 
    %% stack now contains: <array1 element n><array 2 element n> 
    %% check for equality 
    eq not 
    { 
     (Arrays are not equal!\n) print 
    } if 
    } 
    for 
}{ 
    (Arrays are not equal in length\n) print 
} ifelse 
を確認し、実施例2の長

%! 
%% First let us define two arrays of differing lengths 

userdict begin  %% We'll define these in user dict 
/Array1 [0 0 0] def 
/Array2 [0 1] def 

% So when testing compound objects for equality, we should first 
% start by checking the lengths (sizes) of the two objects 

Array1 length % Put array1 on the stack then call the 'length' operator 
       % stack now contains the length of Array1 
Array2 length % Put array2 on the stack then call the 'length' operator 
       % stack now contains the lengths of Array1 and Array2 
eq    % The eq operator tests the two objects on the stack to 
       % see if they are equal and returns a boolean 
       % stack now contains a boolean 

% So now we declare some executable arrays, each executable array 
% can be thought of as an inline function. We define one for each possible 
% value; true or false 
{ 
    (Array1 and Array2 are equal!\n) print 
} 
{ 
    (Array1 and Array2 are not equal!\n) print 
} 

% The ifelse operator consumes two executable arrays, and a boolean, from 
% the operand stack. If the boolean is true it executes the first 
% array, otherwise it executes the second. 
ifelse 

をチェック

ここには明らかな理由がいくつかあります。配列は単なるコンテナなので、別の配列や辞書、文字列などを含む配列を避けることはできません。

これを処理するには、等価性をテストするための関数をいくつか定義することをお勧めします必要に応じて、再帰的に呼び出すことができます。

上記の関数は、成功または失敗を示すために何も返しません(バックチャネルの出力を除く)。明らかにブール値の結果が必要です。これを行う最も簡単な方法は、スタックに「真」を当てることです。平等に失敗した場合はtrueをポップし、それをfalseに置き換えます。

機能は、それが不平等を見つけたとき、終了オペレータは、(おそらくかかわらず、最初に上記のブール値を実装したいと思う)

最後に、関数は非効率的であることを行うために使用することができ終了しません。現在の辞書から常に同じオブジェクトをコピーするためです。スタック上のすべての操作を行うために関数を書き直すことが可能です。これはより速くなります。

警告:私は実際にここでのPostScriptプログラムをテストしていませんが、例の機能は、[PostScriptの中で考える]にあります

+1

:-)タイプミスが完全に可能である(http://wwwcdf.pd.infn.it/ localdoc/tips.pdf)、p.33(pdfのp.49)を参照してください。しかし、この答えはまた、関係するすべての部分の優れた説明です。 –