comparing values of range from different arrays

后端 未结 2 796
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-27 10:21

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 c

2条回答
  •  无人及你
    2021-01-27 10:45

    Use a hash:

    my @arr3 = (1..5, 5..10, 10..15); 
    my @arr4=(3..7, 9..12, 14..17);
    
    my (%nums1, %nums2);
    
    $nums1{$_} = 1 foreach(@arr3);
    $nums2{$_} = 1 foreach(@arr4);
    
    foreach (sort { abs $a <=> abs $b} keys %nums1){
        print "$_\n" if exists $nums2{$_} 
    }
    

提交回复
热议问题