我之前已定义
/smth1 [0 1 0] def
/smth2 [-1 0 0] def
我需要检查它们是否相等,如果是,请执行一些操作......
例如,(Equal!) show
。
我知道我应该使用eq
,可能还有
... {(Equal!) show} if
但我无法弄清楚如何正确比较之前定义的 smth1 和 smth2 。
请告知。
答案 0 :(得分: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
如果你运行它,你会看到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
现在这里有一些明显的推论;数组只是容器,没有什么可以阻止包含另一个数组,字典或字符串的数组......
为了解决这个问题,最好定义一些函数来测试相等性,并根据需要调用它们,可能是递归的。
上述功能不会返回任何指示成功或失败的信息(反向通道上的输出除外)。显然,需要一个布尔结果。最简单的方法就是坚持“真实”。在堆栈上,如果相等失败,则弹出true并将其替换为false。
函数在找到不等式时不会终止,退出运算符可以用来做(你可能想先在上面实现布尔值)
最后,该函数效率低下,因为它不断地将相同的对象复制出当前字典。可以重写函数来完成堆栈上的所有操作,这样会更快。
警告:我实际上没有在这里测试PostScript程序,错别字是完全可能的: - )