for (my $j=0; $j <= @grammatches; $j++) { ## Could be normal $j++ if use another variable instead of @matches for both ... for (my $l=0; $l <= @headmatches; $l++) { ... for (my $m=0; $m <= @sentmatches; $m++) {