Perl:为了按频率对输出进行排序,我需要将数组中的元素以两个为一组进行连接

问题描述

| 让我知道是否需要更简洁一些,如果您没有时间,请回答上面的问题,然后跳过以下单词: 我只是想让您完全理解问题。提前感谢很多。 背景:我正在制作搜索引擎(这只是代码的一小部分...因此它可能对更改很敏感...不是您的问题)。搜索引擎必须输出所有符合特定条件的句子。因此,如果句子中包含正确的术语,它将被放置在较大标题(大写)下的子标题下。大写的标题按字母顺序排序。我希望子标题按频率排序。 目的:我想到了通过将所有相应的匹配项加入子标题来按频率排序的方法,然后每个子标题元素中最大的一个首先出现。我该怎么做呢? (例如,需要将$ array [0]和$ array [1]连接起来) 道歉:我是Perl和此站点的新手,所以我不确定这种格式是否太长...我确定并很抱歉我的代码很粗糙,但是可以使用 开始了: 传说1:$ sortedallgramfunc的格式为:XCOMP 传说2:$ headmatches(子标题)的形式为:xcomp的支出为:移动 传说3:$ sentmatches的格式为:MATCH#1已发送。 29 为了获得这些有机分子,动物必须(消耗)能量来移动自己 如果子标题也具有MATCH#2,则该子标题和所有关联的句子应在只有1个匹配项的子标题之前打印
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;
}
    

解决方法

        您可能想要List :: MoreUtil的natatime。这是许多其他问题的重复说明,例如,如何在Perl foreach循环中一次读取两个项目?