## EDIT: Now the EVEN number is gramfunc, ODD number is sentence my @allgramfunc; ## list of unique grammar func my @allmatches; ##use for headings and all matches (sentences) under that heading as one scalar my @sortedallgramfunc; ## What order for capital heading? => alphabetical my @sortedheadmatches; ## What order for dependency heading? => frequency Depend on firstword? my @sortedfirstmatches; ## To keep order of sentences with headmatches my @sortedsecondmatches; ## To keep order of sentences with headmatches my %seenmatches = (); my %seens = (); #my @pluralfirstmatches = @firstmatches; #my @pluralsecondmatches = @secondmatches; #CREATE an array of all the grammar functions: for (my $j=0; $j <= @grammatches; $j++) { ## Could be normal $j++ if use another variable instead of @matches for both if ( defined( $grammatches[$j] )) { ## Just to avoid error message push (@allgramfunc, "$grammatches[$j]") unless ($seengramfunc{ $grammatches[$j] }++); } } #SORT overheadings by alphabetical @sortedallgramfunc = sort { lc($a) cmp lc($b) } @allgramfunc; #PRINT all the sentences which are related to searchkey by the same gramfunc foreach my $sortedallgramfunc (@sortedallgramfunc) { print ("\n",uc $sortedallgramfunc,"\n\n");# Which gramfunc is being shown? #@sortedheadmatches = sort @headmatches; for (my $l=0; $l <= @headmatches; $l++) { if (defined( $headmatches[$l] ) and $headmatches[$l] =~ /$sortedallgramfunc/) { #2#$pluralfirstmatches[$l] =~ s/$firstmatches[$l]$pluralsuffix/$firstmatches[$l]/ig; unless ($seenmatches{ $headmatches[$l] }++) { print $headmatches[$l]; my $count = 1; for (my $m=0; $m <= @sentmatches; $m++) { if ( defined( $sentmatches[$m]) and $sentmatches[$m] =~ /\s\S\S$firstmatches[$l]\S\S\s/ and $sentmatches[$m] =~ /\s\S\S$secondmatches[$l]\S\S\s/) { ##We know $l and $m are matching #5# Try sorting by creating array that includes header and all sentences as a scalar, then by size, maybe join until hit _dobj sort length $a cmp length $b maybe "length first then alphabetical" => or $a cmp $b print "MATCH #$count $sentmatches[$m]"; # unless $seens{ $sentmatches[$m] }++); $count++; } } } } } }