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