编写脚本以显示一组变量的每个排列

时间:2014-07-02 04:28:43

标签: perl scripting combinations

我正在尝试编写一个perl脚本,它可以准确地确定变量集中可能有多少个排列。有问题的变量有一定数量的选项。

例如:

Var1 [Yes|No]
Var2 [Yes|No|Maybe]
Var3 [1|2|3|4]
Var4 [Yes|No]
Var5 [Yes|No|Maybe]

但是,大约有15个不同的变量,这些变量可能会发生变化。任何人都可以提供使用什么逻辑的一般概念,以便找到这个数据集的每个排列吗?

我能想到的唯一方法是将所有内容存储在数据结构中并使用嵌套循环,但每次我尝试编写内容时都会变得混乱。

提前致谢。

3 个答案:

答案 0 :(得分:2)

使用数组@c存储用于从@arr检索值的索引,

my @arr = (
  [qw/Yes No/],
  [qw/Yes No Maybe/],
  [qw/1 2 3 4/],
  [qw/Yes No/],
  [qw/Yes No Maybe/],
);
my @c = (0) x @arr;

ITER: while (1) {
  my @comb = map $arr[$_][ $c[$_] ],  0 .. $#arr;
  print join("|", @comb), "\n";

  my $i = 0;
  ADVANCE_I: {
    if (++$c[$i] > $#{ $arr[$i] }) {
      $c[$i] =0;
      last ITER if ++$i > $#arr;
      redo ADVANCE_I;
    }
  }
}

答案 1 :(得分:1)

可以使用简单的递归:

use strict;
use warnings;

my @options = map {/\[(.*?)\]/ ? [split /\|/, $1] : die $_} <DATA>;

alloptions(\@options);

sub alloptions {
    my $options = shift;
    my $index = shift || 0;
    my $vals = shift || [];

    for my $val (@{ $options->[$index] }) {
        if ($index == $#$options) {
            print "@$vals $val\n";
        } else {
            alloptions($options, $index + 1, [@$vals, $val]);
        }
    }
}

__DATA__
Var1 [Yes|No]
Var2 [Yes|No|Maybe]
Var3 [1|2|3|4]
Var4 [Yes|No]
Var5 [Yes|No|Maybe]

输出:

Yes Yes 1 Yes Yes
Yes Yes 1 Yes No
Yes Yes 1 Yes Maybe
Yes Yes 1 No Yes
Yes Yes 1 No No
Yes Yes 1 No Maybe
Yes Yes 2 Yes Yes
Yes Yes 2 Yes No
Yes Yes 2 Yes Maybe
Yes Yes 2 No Yes
Yes Yes 2 No No
Yes Yes 2 No Maybe
Yes Yes 3 Yes Yes
Yes Yes 3 Yes No
Yes Yes 3 Yes Maybe
Yes Yes 3 No Yes
Yes Yes 3 No No
Yes Yes 3 No Maybe
Yes Yes 4 Yes Yes
Yes Yes 4 Yes No
Yes Yes 4 Yes Maybe
Yes Yes 4 No Yes
Yes Yes 4 No No
Yes Yes 4 No Maybe
Yes No 1 Yes Yes
Yes No 1 Yes No
Yes No 1 Yes Maybe
Yes No 1 No Yes
Yes No 1 No No
Yes No 1 No Maybe
Yes No 2 Yes Yes
Yes No 2 Yes No
Yes No 2 Yes Maybe
Yes No 2 No Yes
Yes No 2 No No
Yes No 2 No Maybe
Yes No 3 Yes Yes
Yes No 3 Yes No
Yes No 3 Yes Maybe
Yes No 3 No Yes
Yes No 3 No No
Yes No 3 No Maybe
Yes No 4 Yes Yes
Yes No 4 Yes No
Yes No 4 Yes Maybe
Yes No 4 No Yes
Yes No 4 No No
Yes No 4 No Maybe
Yes Maybe 1 Yes Yes
Yes Maybe 1 Yes No
Yes Maybe 1 Yes Maybe
Yes Maybe 1 No Yes
Yes Maybe 1 No No
Yes Maybe 1 No Maybe
Yes Maybe 2 Yes Yes
Yes Maybe 2 Yes No
Yes Maybe 2 Yes Maybe
Yes Maybe 2 No Yes
Yes Maybe 2 No No
Yes Maybe 2 No Maybe
Yes Maybe 3 Yes Yes
Yes Maybe 3 Yes No
Yes Maybe 3 Yes Maybe
Yes Maybe 3 No Yes
Yes Maybe 3 No No
Yes Maybe 3 No Maybe
Yes Maybe 4 Yes Yes
Yes Maybe 4 Yes No
Yes Maybe 4 Yes Maybe
Yes Maybe 4 No Yes
Yes Maybe 4 No No
Yes Maybe 4 No Maybe
No Yes 1 Yes Yes
No Yes 1 Yes No
No Yes 1 Yes Maybe
No Yes 1 No Yes
No Yes 1 No No
No Yes 1 No Maybe
No Yes 2 Yes Yes
No Yes 2 Yes No
No Yes 2 Yes Maybe
No Yes 2 No Yes
No Yes 2 No No
No Yes 2 No Maybe
No Yes 3 Yes Yes
No Yes 3 Yes No
No Yes 3 Yes Maybe
No Yes 3 No Yes
No Yes 3 No No
No Yes 3 No Maybe
No Yes 4 Yes Yes
No Yes 4 Yes No
No Yes 4 Yes Maybe
No Yes 4 No Yes
No Yes 4 No No
No Yes 4 No Maybe
No No 1 Yes Yes
No No 1 Yes No
No No 1 Yes Maybe
No No 1 No Yes
No No 1 No No
No No 1 No Maybe
No No 2 Yes Yes
No No 2 Yes No
No No 2 Yes Maybe
No No 2 No Yes
No No 2 No No
No No 2 No Maybe
No No 3 Yes Yes
No No 3 Yes No
No No 3 Yes Maybe
No No 3 No Yes
No No 3 No No
No No 3 No Maybe
No No 4 Yes Yes
No No 4 Yes No
No No 4 Yes Maybe
No No 4 No Yes
No No 4 No No
No No 4 No Maybe
No Maybe 1 Yes Yes
No Maybe 1 Yes No
No Maybe 1 Yes Maybe
No Maybe 1 No Yes
No Maybe 1 No No
No Maybe 1 No Maybe
No Maybe 2 Yes Yes
No Maybe 2 Yes No
No Maybe 2 Yes Maybe
No Maybe 2 No Yes
No Maybe 2 No No
No Maybe 2 No Maybe
No Maybe 3 Yes Yes
No Maybe 3 Yes No
No Maybe 3 Yes Maybe
No Maybe 3 No Yes
No Maybe 3 No No
No Maybe 3 No Maybe
No Maybe 4 Yes Yes
No Maybe 4 Yes No
No Maybe 4 Yes Maybe
No Maybe 4 No Yes
No Maybe 4 No No
No Maybe 4 No Maybe

答案 2 :(得分:-1)

由于您没有提出具体问题,我认为您需要帮助才能找到算法?

  1. 对于每一行,
    1. 解析该行以提取令牌。
    2. 对于CPAN上任何一个易于查找的功能返回的每个排列,
      1. 打印排列。
  2. 如果您需要任何特定方面的帮助,请告诉我们您的尝试,以便我们帮助您解决问题。