我有一个大约有25000条记录的文件,每个记录都有超过13个条目是药物名称。我想为这些条目形成所有可能的配对组合。例如:如果一行有三个记录A,B,C,我应该形成如下的组合: 1) A B 2) A C 3)B C。下面是我从互联网上得到的代码,它只有在单行分配给一个数组的情况下才有效:
use Math::Combinatorics;
my @n = qw(a b c);
my $combinat = Math::Combinatorics->new(
count => 2,
data => [@n],
);
while ( my @combo = $combinat->next_combination ) {
print join( ' ', @combo ) . "\n";
}我使用的代码,它不会产生任何输出:
open IN, "drugs.txt" or die "Cannot open the drug file";
open OUT, ">Combination.txt";
use Math::Combinatorics;
while (<IN>) {
chomp $_;
@Drugs = split /\t/, $_;
@n = $Drugs[1];
my $combinat = Math::Combinatorics->new(
count => 2,
data => [@n],
);
while ( my @combo = $combinat->next_combination ) {
print join( ' ', @combo ) . "\n";
}
print "\n";
}你能给我一个解决这个问题的建议吗?
发布于 2012-06-12 13:20:31
您正在将@n设置为包含@Drugs数组的第二个值的数组,请尝试在Math::Combinatorics构造函数中使用data => \@Drugs。
同样,使用严格;使用警告;等等。
发布于 2012-06-12 22:10:34
我以前也为别人回答过类似的问题。对于他们来说,他们有一个关于如何将字母列表组合成所有可能的单词的问题。
看看How Can I Generate a List of Words from a group of Letters Using Perl吧。在其中,您将看到一个从my answer使用Math::Combinatorics的示例,以及ikegami拥有的 answer。(他用正则表达式做了一些相当有趣的事情)。
我相信其中一个会带你找到你想要的答案。也许当我有更多的时间时,我会专门为你的问题提供一个具体的答案。我希望这个链接能有所帮助。
https://stackoverflow.com/questions/10990357
复制相似问题