PostScript - 比较相等值 + "if" 结构

PostScript - Comparing values for equality + "if" structure

我之前定义过

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

我需要检查它们是否相等,如果相等,则执行一些操作...

例如,(Equal!) show.

我知道我应该使用 eq 并且可能使用

... {(Equal!) show} if 

但是我不知道如何正确比较之前定义的smth1smth2

请指教

你不想比较数组,你想比较数组的内容。可以在 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

如果你 运行 那,你会看到 Array1 和 Array2 不相等,但 Array1 和 Pointer 相等。那是因为 Pointer 是(松散地)指向 Array1 的指针。事实上,按照 PostScript 的工作方式,两者都是对同一对象的引用。 Array1 和 Array2 是对不同对象的引用,即使它们的内容相同。

所以在您的情况下,您想要检索数组的每个元素,并将其与另一个数组中的相同元素进行比较。如果不相等则中止,否则继续。

我们将使用的有用运算符:length、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

现在这里有一些明显的推论;数组只是容器,没有什么可以阻止一个数组包含另一个数组、字典或字符串.....

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

上面的函数 return 没有任何指示成功或失败的信息(后台通道上的输出除外)。显然需要布尔结果。最简单的方法是在堆栈上粘贴一个 'true',如果相等失败,弹出 true 并用 false 替换它。

函数在发现不等式时不会终止,exit 运算符可用于执行此操作(尽管您可能希望先实现上面的布尔值)

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

警告:我还没有实际测试过这里的 PostScript 程序,完全有可能出现错别字:-)