while (@lines > 12) { # check first 7 lines against the 8th line my @vals = split /\s+/, $lines[12]; my @chk = split /\s+/, join(' ', @lines[0..11]); my %match; foreach my $val (@vals) { $match{$val}++ if grep { $val eq $_ } @chk; } my @match = sort keys %match; my @nomatch = grep { not exists $match{$_} } @vals; my $match = @match; my $nomatch = @nomatch; #my $vals = @vals; #my $lines = @lines; #print NUMBER "$lines @lines \n\n"; if ($match == 0) { $zero ++ } if ($match == 1) { $one ++ } if ($match == 2) { $two ++ } if ($match == 3) { $three ++ } if ($match == 4) { $four ++ } if ($match == 5) { $five ++ } if ($match == 6) { $six ++ } # do whatever you want with the matches and no-matches print MATCH "$line: \tmatch = @match\n"; print NOMATCH "$line: \tnomatch = @nomatch\n"; $line++; # get rid of first line so next loop will be 2-6 and so on shift @lines; }