所以我的代码设置如下
#!/usr/bin/perl -w
# vim: set expandtab ts=2 bg=dark smartindent shiftwidth=2 softtabstop=2 :
#
use strict;
use Data::Dumper;
use Getopt::Long qw(GetOptions);
####use warnings;
use 5.010;
####my @arr1 = ( "0", "1", "2", "3", "4"); # OK
my @arr1 = ( "0", "1", "3", "4"); # Gap-1... seq2
my $arr1_len = scalar @arr1;
##
my @arr2 = ( "0", "1", "2", "3", "4"); # OK
####my @arr2 = ( "0", "1", "3", "4"); # Gap-2... seq2
my $arr2_len = scalar @arr2;
我正在尝试遍历每个列表,并将其与彼此进行比较,并检测序列中的任何间隔(这些值是每个流的seq数字-目前它们是顺序的,但它们不必/不需要在真实的示例)
EFFORT 1 =如果我为arr2使用一个内部for循环,它总是从indx 0开始-也就是说,在我们匹配之后指针/计数不会打勾
EFFORT 2 =如果我执行了一个虚假的内部/ arr2循环,那么在不增加i计数器的情况下我无法进入下一个j
...我怀疑必须有一种简单的方法来执行此操作-但我无法弄清楚
EFFORT 1
ARR1: for (my $i=0; $i <= ($arr1_len-1); $i++) {
print "i[$i]=$arr1[$i]\n";
ARR2: for (my $j=0; $j <= ($arr2_len-1); $j++) {
print "... j[$j]=$arr2[$j] -- ((i[$i]=$arr1[$i]))\n";
# seq num match
if ( $arr1[$i] eq $arr2[$j]){
print "MATCH of seq_num [$arr1[$i]|$arr2[$j]]\n";
shift @arr2;
next ARR1;
} # end of seq num match
# gap in arr2
if ( $arr1[$i] < $arr2[$j]){
print "GAP in new [$arr1[$i]|$arr2[$j]]... New Missing $arr1[$i]\n";
next ARR1;
} # end of gap in arr2
# gap in arr1
if ( $arr2[$j] < $arr1[$i]){
print "GAP in old [$arr1[$i]|$arr2[$j]]... Old Missing $arr2[$j]\n";
next ARR2;
} # end of gap in arr1
} # end of j loop
} # i loop
EFFORT 2
my $j = 0;
ARR1: for (my $i=0; $i <= ($arr1_len-1); $i++) {
print "i[$i]=$arr1[$i]\n";
if ( $j <= ($arr2_len-1) ) {
print "... j[$j]=$arr2[$j] -- ((i[$i]=$arr1[$i]))\n";
# seq num match
if ( $arr1[$i] eq $arr2[$j]){
print "MATCH of seq_num [$arr1[$i]|$arr2[$j]]\n";
$j++;
next ARR1;
} # end of seq_num match
# probable gap in arr2
if ( $arr1[$i] < $arr2[$j]){
print "GAP in new [$arr1[$i]|$arr2[$j]]... New Missing $arr1[$i]\n";
next ARR1;
} # end of gap in arr2
# probable gap in arr1
if ( $arr2[$j] < $arr1[$i]){
print "GAP in old [$arr1[$i]|$arr2[$j]]... arr1 Missing $arr2[$j]\n";
# CANT NEXT J WITHOUT INCREASING I ?!?
} # end of gap in arr1
} # end of fake j loop!
} # end of i loop
答案 0 :(得分:0)
仅当孔或间隙位于arr1而不是arr2时才定义孔-或反之亦然...并且我需要以迭代的方式解决这个问题(例如,当有匹配项时,我会做一些更多检查这些数据对象)
哈希表是您的朋友。像这样的片段:
use List::Util qw/max/;
my %hash1 = map { $_ => 1 } @arr1;
my %hash2 = map { $_ => 1 } @arr2;
my $len = max($arr1[$#arr1], $arr2[$#arr2]);
for my $n (0 .. $len) {
if (exists $hash1{$n} and exists $hash2{$n}) {
# n is in both lists
} elsif (exists $hash1{$n}) {
# n is only in the first one.
} elsif (exists $hash2{$n}) {
# n is only in the second one.
} else {
# Not in either one
}
}
可能会帮您解决这个问题。
答案 1 :(得分:-1)