If statement PostScript-比较相等值+&引用;如果;结构

If statement PostScript-比较相等值+&引用;如果;结构,if-statement,equality,postscript,If Statement,Equality,Postscript,我以前定义过 /smth1 [0 1 0] def /smth2 [-1 0 0] def 我需要检查它们是否相等,如果相等,执行一些操作 例如,(相等!)显示 我知道我应该使用eq,可能还有类似的东西 ... {(Equal!) show} if 但我不知道如何正确比较之前定义的smth1和smth2 请告知。您不想比较数组,而是想比较数组的内容。可以在PostScript中测试数组和其他复合对象是否相等,但这并不测试它们的内容,只测试它们是否是同一对象 例如: 如果运行它,您将看到Ar

我以前定义过

/smth1 [0 1 0] def
/smth2 [-1 0 0] def
我需要检查它们是否相等,如果相等,执行一些操作

例如,
(相等!)显示

我知道我应该使用
eq
,可能还有类似的东西

... {(Equal!) show} if 
但我不知道如何正确比较之前定义的smth1和smth2


请告知。

您不想比较数组,而是想比较数组的内容。可以在PostScript中测试数组和其他复合对象是否相等,但这并不测试它们的内容,只测试它们是否是同一对象

例如:

如果运行它,您将看到Array1和Array2不相等,但Array1和指针相等。这是因为指针(松散地)是指向数组1的指针。事实上,PostScript的工作方式都是对同一对象的引用。而Array1和Array2是对不同对象的引用,即使它们的内容相同

因此,在本例中,您希望检索数组的每个元素,并将其与另一个数组中的相同元素进行比较。如果它们不相等,则中止,否则继续

我们将使用的有用运算符:长度、for、eq、get、dup、exch、if、ifelse

以下示例并非有效的解决方案,但应为您提供处理此问题的方法:

例1,检查长度

%!
%% 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
示例2,现在检查内容

%!
%% 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
%!
%%首先让我们定义两个具有相同内容的数组
userdict begin%%我们将在user dict中定义这些
/阵列1[0]def
/阵列2[0]def
阵列1长度阵列2长度等式
{
%“for”运算符使用4个操作数,即循环计数器的初始值,
%每次通过时计数器递增的量,以及终止
%计数器的值,最后是每次传递时要执行的可执行数组。
%因此,从循环计数=0开始,每次递增1,然后停止
%当计数器是数组的长度时。注意!因为我们从0开始
%计数器是数组长度-1。
0 1阵列1长度1子节点
{
%%现在,在每次传递时,堆栈上的顶部元素是循环计数器
%%我们需要两次,每个数组一次。所以我们从
%%复制并将其放在堆栈上
重复
%%堆栈现在包含:
%%现在从第一个数组中获取第n个元素:
收到
%%堆栈现在包含:
%%我们想使用循环计数器来索引第二个数组,但它不是
%%在堆栈顶部,交换顶部的两个元素:
exch
%%堆栈现在包含:
%%现在使用计数器从第二个数组中获取第n个元素
收到
%%堆栈现在包含:
%%检查是否相等
情商不
{
(数组不相等!\n)打印
}如果
} 
对于
}{
(数组长度不相等\n)打印
}如果有
现在这里有一些明显的推论;数组只是容器,没有什么可以阻止数组包含另一个数组、字典或字符串

为了解决这个问题,最好定义一些函数来测试相等性,并根据需要调用它们,可能是递归的

上述函数不返回任何指示成功或失败的信息(后台通道上的输出除外)。显然,需要一个布尔结果。最简单的方法是在堆栈上粘贴一个“true”,如果相等失败,则弹出true并替换为false

函数在发现不等式时不会终止,可以使用exit操作符来完成这一操作(不过您可能需要首先实现上面的布尔值)

最后,该函数效率低下,因为它不断地从当前字典中复制相同的对象。可以重写函数来执行堆栈上的所有操作,这会更快


警告:我还没有在这里测试PostScript程序,完全可能出现打字错误:-)

第33页(pdf第49页)中有一个函数示例。但这个答案也很好地解释了所有涉及的部分。
%!
%% 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