更新时间:2022-06-21 23:56:28
首先,你的 2 个数组需要正确写入.
First of all, your 2 arrays need to be written correctly.
@a = ("abc","def","efg","ghy","klm","ghn");
@b = ("def","efg","ghy","klm","ghn","klm");
第二,对于任意数组(例如,其元素可能引用其他数据结构的数组),您可以使用 Data::Compare
.
Second of all, for arbitrary arrays (e.g. arrays whose elements may be references to other data structures) you can use Data::Compare
.
对于元素为标量的数组,您可以使用 List::MoreUtils 进行比较
pairwise BLOCK ARRAY1 ARRAY2
,其中 BLOCK 是您的比较子程序.您可以通过以下方式模拟 pairwise
(如果您没有 List::MoreUtils 访问权限):
For arrays whose elements are scalar, you can do comparison using List::MoreUtils
pairwise BLOCK ARRAY1 ARRAY2
, where BLOCK is your comparison subroutine. You can emulate pairwise
(if you don't have List::MoreUtils access) via:
if (@a != @b) {
$equals = 0;
} else {
$equals = 1;
foreach (my $i = 0; $i < @a; $i++) {
# Ideally, check for undef/value comparison here as well
if ($a[$i] != $b[$i]) { # use "ne" if elements are strings, not numbers
# Or you can use generic sub comparing 2 values
$equals = 0;
last;
}
}
}
附言我不确定,但 List::Compare 可能总是对列表进行排序.我不确定它是否可以进行成对比较.
P.S. I am not sure but List::Compare may always sort the lists. I'm not sure if it can do pairwise comparisons.