为什么Perl的Math :: Combinatorics抱怨"必须使用'频率'参数的next_permutation而不传递给构造函数"?

vas*_*sin 2 perl permutation

我正在尝试使用Math :: Combinatorics生成数组的唯一排列.正如CPAN页面所说,可以使用next_string()完成:

use Math::Combinatorics;
my @arr = [1,1,1,0,0];  
$c = Math::Combinatorics->new( count=>5, data=>[\@arr], frequency=>[3,2] );
while (@permu = $c->next_string()){
print "@permu\n";
}  
Run Code Online (Sandbox Code Playgroud)

但是这段代码给了我以下错误:必须使用未传递给构造函数的'frequency'参数的next_permutation,我无法理解为什么.

bri*_*foy 6

你在那个项目中遇到很多问题.您的数据类型不匹配.

如果要使用frequency,只需指定一次唯一元素,但指定它们出现的次数.您为频率提供的数组引用必须与数据数组的长度相同:

use Math::Combinatorics;

my @array = (1,0); # an array, not an array reference

$c = Math::Combinatorics->new( 
    count     => 5,
    data      => \@array,        # now you take a reference    
    frequency => [3,2] 
    );

while (@permu = $c->next_string ){
    print "@permu\n";
    }
Run Code Online (Sandbox Code Playgroud)

现在你应该得到你想要的输出,这是你不能分辨多个1和多个0的区别的不同组合:

0 1 1 1 0
0 1 1 0 1
0 1 0 1 1
0 0 1 1 1
1 0 1 1 0
1 0 1 0 1
1 0 0 1 1
1 1 0 1 0
1 1 0 0 1
1 1 1 0 0
Run Code Online (Sandbox Code Playgroud)

如果不使用frequency,则只需指定数据数组中的所有元素.但是,您可能正在避免这种情况,因为它将每个元素视为不同的元素,因此它不会崩溃看起来像是相同的组合.