0

I tried to read two files and compare them:

file1 : AAAAAAAAAA

file2: AAAABAAAAA

output: MMMMNMMMMM

open(my $fh1, '<', 'file1');

open(my $fh2, '<', 'file2');


while(
    defined(my $line1 = <$fh1>)
    and
    defined(my $line2 = <$fh2>)
    ){
    chomp $line1;
    chomp $line2;
    my @line1 = split(//, $line1);
    my @line2 = split(//, $line2);  
        for my $i (0 ..@line1-1){
            for my $j (0 .. @line2-1){
                if ($line1[$i] eq $line2[$j]){      
                    print "M\";}        

                    else {          
                    print "N";} 
        $j++;}
        $i++;}}

close $fh1;
close $fh2;

It prints output repeatedly!! If somebody help me it would be a great help.

4

1 回答 1

1

You need only one for loop per line,

my $max = $#line1 > $#line2 ? $#line1 : $#line2;
for my $i (0 .. $max) {
  if ($line1[$i] eq $line2[$i]) { print "M";}
  else                          { print "N";} 
}
于 2013-10-04T12:53:56.980 回答