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

时间:2012-06-12 03:54:42

标签: perl

我有一个大约有25000条记录的文件,每条记录有超过13个条目都是药品名称。我想为这些条目形成所有可能的对组合。例如:如果一条线有三条记录A,B,C。我应该形成组合1)AB 2)AC 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";
}

你能否建议我解决这个问题?

3 个答案:

答案 0 :(得分:1)

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

另外,使用严格;使用警告; blahblahblah。

答案 1 :(得分:1)

数组中的所有对都可以直接计算。从您的问题中使用药物A,B和C,您可能会认为它们形成了一个方阵。

AA  AB  AC
BA  BB  BC
CA  CB  CC

您可能不希望“对角线”对AA,BB和CC。请注意,其余元素是对称的。例如,元素(0,1)是AB,(1,0)是BA。在这里,我再次假设它们是相同的,你不需要重复。

要从线性代数中借用一个术语,你需要upper triangle。这样做可以通过构造消除重复,假设给定线上的每个药物名称都是唯一的。对此的算法如下。

  1. 依次选择每种药物 q 。对于其中每个,请执行步骤2和3.
  2. 与药物开始后立即 q 然后对于每种药物的 - [R 在列表中的剩余部分,则执行步骤3。
  3. 记录对( q r )。
  4. 记录的列表是所有唯一对的列表。
  5. 在Perl中,这看起来像

    #! /usr/bin/env perl
    
    use strict;
    use warnings;
    
    sub pairs {
      my @a = @_;
    
      my @pairs;
      foreach my $i (0 .. $#a) {
        foreach my $j ($i+1 .. $#a) {
          push @pairs, [ @a[$i,$j] ];
        }
      }
    
      wantarray ? @pairs : \@pairs;
    }
    
    my $line = "Perlix\tScalaris\tHashagra\tNextium";
    for (pairs split /\t/, $line) {
      print "@$_\n";
    }
    

    输出:

    Perlix Scalaris
    Perlix Hashagra
    Perlix Nextium
    Scalaris Hashagra
    Scalaris Nextium
    Hashagra Nextium

答案 2 :(得分:0)

我之前已经为别人回答了这样的事情。对于他们来说,他们有一个关于如何将一系列字母组合成所有可能的单词的问题。

看看How Can I Generate a List of Words from a group of Letters Using Perl。在其中,您会看到使用Math::Combinatorics中的my answercorrect answer所拥有的ikegami的示例。 (他用正则表达式做了一些相当有趣的事情)。

我相信其中一项会引导您获得所需的答案。也许当我有更多时间时,我会专门针对你的问题充实答案。我希望这个链接有所帮助。