Perl 形成字符串随机字符串组合

标签 perl

我有一个包含大约 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";
}

您能建议我解决这个问题吗?

最佳答案

您将 @n 设置为包含 @Drugs 数组第二个值的数组,请尝试仅使用 data =>\@Drugs 在 Math::Combinatorics 构造函数中。

另外,使用严格;使用警告;巴拉巴拉巴拉。

关于Perl 形成字符串随机字符串组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10990357/

相关文章:

perl - 在 ActivePerl 上安装 Padre (IDE)?

perl - 不能用 Perl 的 Net::Blogger 发帖

Perl:YAML:Hash 如何选择条目?

perl - 在 directory.pl 第 2 行的 @INC(@INC 包含 : D:/tools/lib .)中找不到 File/Glob.pm

perl - 为什么 "keys::"不是语法错误?

xcode - 全新(干净)安装的 Lion 没有开发人员工具,Xcode 4.3 不再包含 make 和friends

perl - 多客户端全双工 Perl 套接字

mysql - 无法使用 perl DBI 模块连接到 mysql

perl - 为什么这个简单的 Perl push/pop 程序不工作?

regex - 有没有办法在Perl中预编译正则表达式?