Perl: I need to join elements of an array in sets of two in order to sort output by frequency
Let me know if I have to make this more concise, if you don't have time, just answer the question above and skip the following words:
I just wanted you to fully understand the problem. THANKS A LOT in advance.
BACKGROUND: I am making search engine (this is only a small part of the code... so it may be sensitive to change... not your problem). The search engine has to ouput all the sentences meeting a certain criteria. So if the sentence includes the right terms, it will be placed under a sub heading which is under a larger heading(in caps). The caps heading is sorted by alphabetical. I want the sub headings sorted by frequency.
OBJECTIVE: I thought of sorting by frequency by joining all the corresponding matches to the subheading, and then which开发者_JAVA百科 every sub heading element is largest goes first. HOW DO I DO THIS? (Ex. $array[0] and $array[1] need to be concatenated)
APOLOGY: I am new to Perl and this site, so I'm not sure if this format is too long... I am sure and sorry that my code is crude, but it works
HERE WE GO:
LEGEND1: $sortedallgramfunc is of the form: XCOMP
LEGEND2: $headmatches (the subheading) is of the form: xcomp of expend is: move
LEGEND3: $sentmatches is of the form: MATCH #1 Sent. 29 To acquire these organic molecules , animals must --expend-- energy to move themselves
If the subheading has MATCH #2 as well, it and all the sentences associated, should be printed before a subheading with only 1 match
foreach my $sortedallgramfunc (@sortedallgramfunc) {
my @sepmatches; ## MOVED DOWN HERE TO TEST, MAYBE MOVE BACK depending on sol'n
print ("\n",uc $sortedallgramfunc,"\n\n");# Which gramfunc is being shown?
for (my $l=0; $l <= @headmatches; $l++) {
if (defined( $headmatches[$l] ) and $headmatches[$l] =~ /$sortedallgramfunc/) {
unless ($seenmatches{ $headmatches[$l] }++) {
push (@sepmatches, $headmatches[$l]);
my $count = 1;
my @allsents; ## use for all sents that go under heading, add to heading to get @allmatches
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
push (@allsents, "MATCH #$count $sentmatches[$m]"); # unless $seens{ $sentmatches[$m] }++);
$count++;
}
}
push (@sepmatches, @allsents);
##$sepmatches[0] is header, [1] is all sentences etc. EVEN - header, ODD - sent
## NOW WANT TO join headers and sentences (0 and 1 etc.) in @allmatches<====
# SO I can us the following line to hopefully sort by frequency of matches under each subheading: @sortedallmatches = sort {length $a cmp length $b } @allmatches;
}
}
}print @sepmatches;
}
You likely want List::MoreUtil's natatime. This is a duplicate of many other questions along the same lines, such as How do I read two items at a time in a Perl foreach loop?.
精彩评论