比较来自不同数组的范围值 [英] comparing values of range from different arrays

查看:65
本文介绍了比较来自不同数组的范围值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将来自@arr3 的范围值与来自@arr4 的范围值进行比较,但我没有得到所需的输出.请建议我修改以下代码以获得输出3、4、5、6、7、9、10、11、12、14、15(不重复值,例如5和10)和总匹配数=11

i am trying to compare values of range from @arr3 with values of range from @arr4 but i am not getting the desired output. please suggest me the modifications in the following code to get the output as 3,4,5,6,7,9,10,11,12,14,15 (without repeating the values eg 5 and 10) and total matched=11

my @arr3=(1..5,5..10,10..15); 
my @arr4=(3..7,9..12,14..17);

foreach my $line1 (@arr4) {
    my ($from1,$to1)=split/\.\./,$line1;

foreach my $line2 (@arr3) {

    my ($from2,$to2)=split/\.\./,$line2;

    for (my $i=$from1;$i=$to1 ;$i++) {
        for (my $j=$from2;$j=$to2 ;$j++) {
            if ($i==$j) {

                print "$i";print "\n";
            }   
        }
    }   
}
}

推荐答案

注意你的 for 像这样循环

Note that your for loops like this

for (my $i = $from1; $i = $to1; $i++) { ... }

是不正确的,因为第二个子句 $i = $to1 应该是一个 test 以确定循环是否应该继续,而您正在赋值em> $to1$i 的值,所以如果 $to1 为真,循环将永远不会结束.我想您的测试未能完成?

are incorrect, because the second clause $i = $to1 should be a test to determine whether the loop should continue, whereas you are assigning the value of $to1 to $i so the loop will never end if $to1 is true. I imagine your tests have failed to complete?

我还认为您误解了范围运算符 .. 的工作方式.像 2..5 这样的表达式返回一个 list,它包含从 2 到 5 的所有值,并且与 (2, 3, 4, 5).

I also think you are misunderstanding the way the range operator .. works. An expression like 2..5 returns a list consisting of all the values from 2 up to 5, and is identical to (2, 3, 4, 5).

所以你的数组初始化

my @arr3 = (1..5, 5..10, 10..15)

my @arr3 = (
  1, 2, 3, 4, 5,
  5, 6, 7, 8, 9, 10,
  10, 11, 12, 13, 14, 15
)

因此不需要在数组值上使用 split 因为它们已经为您扩展了,并且值 5 和 10 在您的输出中出现两次的原因是因为它们也在输入中存在两次.

and so there is no need to use split on the array values as they have already been expanded for you, and the reason the values 5 and 10 are appearing twice in your output is because they also exist twice in the input.

如果您想查找 两个 数组中的所有唯一值,并且您可以依赖 @arr4 中的值是唯一的(即没有像在 @arr3 中)然后您可以编写类似以下代码的内容.但是如果两个数组可能有重叠,那么您必须明确删除重复项,并且已经发布了使用哈希的解决方案.

If you want to find all unique values that are in both arrays, and you can rely on the values in @arr4 being unique (i.e. there is no overlap like there is in @arr3) then you can write something like the code below. But if both arrays could have overlaps then you have to explicitly remove duplicates, and a solution using a hash which does this has already been posted.

这个解决方案使用了一个label V4 以便next可以去到outer的下一次迭代环形.没有它,即使在找到匹配项之后,也会扫描 @arr3 的其余部分.

This solution uses a label V4 so that the next can go to the next iteration of the outer loop. Without it the rest of @arr3 would be scanned for matches even after one had been found.

use strict;
use warnings;

my @arr3 = (1..5, 5..10, 10..15);
my @arr4 = (3..7, 9..12, 14..17);

V4:
for my $v4 (@arr4) {

  for my $v3 (@arr3) {

    if ($v3 == $v4) {
      print "$v3\n";
      next V4;
    }
  }
}

输出

3
4
5
6
7
9
10
11
12
14
15

这篇关于比较来自不同数组的范围值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆