Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 打印阵列的所有可能组合_Arrays_Perl - Fatal编程技术网

Arrays 打印阵列的所有可能组合

Arrays 打印阵列的所有可能组合,arrays,perl,Arrays,Perl,我想取三个文件,每个文件包含80行或更多的文本,并打印这三个文件的所有可能组合 为了清楚起见,让我们假设 文件1包含 1 2. 3. 文件2包含 4 5. 6. 文件3包含 7 8. 9 如果我只想打印这些组合,那么我可以使用很多答案。但是,由于这些文件包含更多我关心的行,所以手动将它们输入到Perl脚本中。我从每个文件构建一个数组,然后构建这些数组的数组引用。最后,我尝试使用打印组合 我遇到的问题是,我正在打印内存引用,而我不知道如何在List::Permutor调用中取消对数组的引用

我想取三个文件,每个文件包含80行或更多的文本,并打印这三个文件的所有可能组合

为了清楚起见,让我们假设

文件1包含
1
2.
3.
文件2包含
4
5.
6.
文件3包含
7
8.
9
如果我只想打印这些组合,那么我可以使用很多答案。但是,由于这些文件包含更多我关心的行,所以手动将它们输入到Perl脚本中。我从每个文件构建一个数组,然后构建这些数组的数组引用。最后,我尝试使用打印组合

我遇到的问题是,我正在打印内存引用,而我不知道如何在
List::Permutor
调用中取消对数组的引用

Perl代码
使用列表::Permutor;
使用警告;
打开(文件,app_txt);
chomp(@app=());
关闭(文件);
打开(文件,att1_txt);
chomp(@att1=());
关闭(文件);
打开(文件,att2_txt);
咀嚼(@att2=());
关闭(文件);
我的$permutor=List::permutor->new(\@app、\@att1、\@att2);
而(my@permutation=$permutor->next()){
打印“@permutation\n”;
}

不清楚您想要什么

请注意,下面提供的所有代码段输出都用于以下输入:

my @app  = ( 1, 2, 3 );
my @att1 = ( 4, 5, 6 );
my @att2 = ( 7, 8, 9 );

若需要数组的排列,则代码按原样工作

use List::Permutor qw( );

my $permutor = List::Permutor->new( \@app, \@att1, \@att2);
while ( my @permutation = $permutor->next() ) {
   say join ", ", map { "[@$_]" } @permutation;
}
输出:

[1 2 3], [4 5 6], [7 8 9]
[1 2 3], [7 8 9], [4 5 6]
[4 5 6], [1 2 3], [7 8 9]
[4 5 6], [7 8 9], [1 2 3]
[7 8 9], [1 2 3], [4 5 6]
[7 8 9], [4 5 6], [1 2 3]
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 9 8
1 2 3 4 5 6 8 7 9
1 2 3 4 5 6 8 9 7
1 2 3 4 5 6 9 7 8
1 2 3 4 5 6 9 8 7
1 2 3 4 5 7 6 8 9
1 2 3 4 5 7 6 9 8
1 2 3 4 5 7 8 6 9
1 2 3 4 5 7 8 9 6
...
9 8 7 6 5 4 3 2 1
1 4 7
1 4 8
1 4 9
1 5 7
1 5 8
1 5 9
1 6 7
1 6 8
1 6 9
2 4 7
...
3 6 9

如果您想要数组内容的排列(不管字符串来自哪个数组),那么您只需要从数组的内容创建一个列表

use List::Permutor qw( );

my $permutor = List::Permutor->new( @app, @att1, @att2 );
while ( my @permutation = $permutor->next() ) {
   say "@permutation";
}
输出:

[1 2 3], [4 5 6], [7 8 9]
[1 2 3], [7 8 9], [4 5 6]
[4 5 6], [1 2 3], [7 8 9]
[4 5 6], [7 8 9], [1 2 3]
[7 8 9], [1 2 3], [4 5 6]
[7 8 9], [4 5 6], [1 2 3]
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 9 8
1 2 3 4 5 6 8 7 9
1 2 3 4 5 6 8 9 7
1 2 3 4 5 6 9 7 8
1 2 3 4 5 6 9 8 7
1 2 3 4 5 7 6 8 9
1 2 3 4 5 7 6 9 8
1 2 3 4 5 7 8 6 9
1 2 3 4 5 7 8 9 6
...
9 8 7 6 5 4 3 2 1
1 4 7
1 4 8
1 4 9
1 5 7
1 5 8
1 5 9
1 6 7
1 6 8
1 6 9
2 4 7
...
3 6 9
请记住,p(80)是可观测宇宙中原子数的1000000000000000000000000倍


如果希望从每个数组中获得单个元素的所有可能集合,则可以使用以下选项:

for my $app (@app) {
for my $att1 (@att1) {
for my $att2 (@att2) {
   say "$app $att1 $att2";
}}}

输出:

[1 2 3], [4 5 6], [7 8 9]
[1 2 3], [7 8 9], [4 5 6]
[4 5 6], [1 2 3], [7 8 9]
[4 5 6], [7 8 9], [1 2 3]
[7 8 9], [1 2 3], [4 5 6]
[7 8 9], [4 5 6], [1 2 3]
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 9 8
1 2 3 4 5 6 8 7 9
1 2 3 4 5 6 8 9 7
1 2 3 4 5 6 9 7 8
1 2 3 4 5 6 9 8 7
1 2 3 4 5 7 6 8 9
1 2 3 4 5 7 6 9 8
1 2 3 4 5 7 8 6 9
1 2 3 4 5 7 8 9 6
...
9 8 7 6 5 4 3 2 1
1 4 7
1 4 8
1 4 9
1 5 7
1 5 8
1 5 9
1 6 7
1 6 8
1 6 9
2 4 7
...
3 6 9

你想要什么还不清楚

请注意,下面提供的所有代码段输出都用于以下输入:

my @app  = ( 1, 2, 3 );
my @att1 = ( 4, 5, 6 );
my @att2 = ( 7, 8, 9 );

若需要数组的排列,则代码按原样工作

use List::Permutor qw( );

my $permutor = List::Permutor->new( \@app, \@att1, \@att2);
while ( my @permutation = $permutor->next() ) {
   say join ", ", map { "[@$_]" } @permutation;
}
输出:

[1 2 3], [4 5 6], [7 8 9]
[1 2 3], [7 8 9], [4 5 6]
[4 5 6], [1 2 3], [7 8 9]
[4 5 6], [7 8 9], [1 2 3]
[7 8 9], [1 2 3], [4 5 6]
[7 8 9], [4 5 6], [1 2 3]
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 9 8
1 2 3 4 5 6 8 7 9
1 2 3 4 5 6 8 9 7
1 2 3 4 5 6 9 7 8
1 2 3 4 5 6 9 8 7
1 2 3 4 5 7 6 8 9
1 2 3 4 5 7 6 9 8
1 2 3 4 5 7 8 6 9
1 2 3 4 5 7 8 9 6
...
9 8 7 6 5 4 3 2 1
1 4 7
1 4 8
1 4 9
1 5 7
1 5 8
1 5 9
1 6 7
1 6 8
1 6 9
2 4 7
...
3 6 9

如果您想要数组内容的排列(不管字符串来自哪个数组),那么您只需要从数组的内容创建一个列表

use List::Permutor qw( );

my $permutor = List::Permutor->new( @app, @att1, @att2 );
while ( my @permutation = $permutor->next() ) {
   say "@permutation";
}
输出:

[1 2 3], [4 5 6], [7 8 9]
[1 2 3], [7 8 9], [4 5 6]
[4 5 6], [1 2 3], [7 8 9]
[4 5 6], [7 8 9], [1 2 3]
[7 8 9], [1 2 3], [4 5 6]
[7 8 9], [4 5 6], [1 2 3]
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 9 8
1 2 3 4 5 6 8 7 9
1 2 3 4 5 6 8 9 7
1 2 3 4 5 6 9 7 8
1 2 3 4 5 6 9 8 7
1 2 3 4 5 7 6 8 9
1 2 3 4 5 7 6 9 8
1 2 3 4 5 7 8 6 9
1 2 3 4 5 7 8 9 6
...
9 8 7 6 5 4 3 2 1
1 4 7
1 4 8
1 4 9
1 5 7
1 5 8
1 5 9
1 6 7
1 6 8
1 6 9
2 4 7
...
3 6 9
请记住,p(80)是可观测宇宙中原子数的1000000000000000000000000倍


如果希望从每个数组中获得单个元素的所有可能集合,则可以使用以下选项:

for my $app (@app) {
for my $att1 (@att1) {
for my $att2 (@att2) {
   say "$app $att1 $att2";
}}}

输出:

[1 2 3], [4 5 6], [7 8 9]
[1 2 3], [7 8 9], [4 5 6]
[4 5 6], [1 2 3], [7 8 9]
[4 5 6], [7 8 9], [1 2 3]
[7 8 9], [1 2 3], [4 5 6]
[7 8 9], [4 5 6], [1 2 3]
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 9 8
1 2 3 4 5 6 8 7 9
1 2 3 4 5 6 8 9 7
1 2 3 4 5 6 9 7 8
1 2 3 4 5 6 9 8 7
1 2 3 4 5 7 6 8 9
1 2 3 4 5 7 6 9 8
1 2 3 4 5 7 8 6 9
1 2 3 4 5 7 8 9 6
...
9 8 7 6 5 4 3 2 1
1 4 7
1 4 8
1 4 9
1 5 7
1 5 8
1 5 9
1 6 7
1 6 8
1 6 9
2 4 7
...
3 6 9

我通过作弊解决了上一个格式化问题。我刚刚编辑了源文件并删除了所有\r和\n

最终代码:

use Algorithm::Loops qw( NestedLoops );
use warnings;

open (FILE, app_txt);
 ($app = (<FILE>));
close (FILE);
my @app = split /,/, $app;

open (FILE, att1_txt);
 ($att1 = (<FILE>));
close (FILE);
my @att1 = split /,/, $att1;

open (FILE, att2_txt);
 ($att2 = (<FILE>));
close (FILE);
my @att2= split /,/, $att2;

my $iter = NestedLoops([ \@app, \@att1, \@att2 ]);
while ( my ($app, $att1, $att2) = $iter->() ) {
    open (my $fh, '>', 'test');
    print  $fh "$app $att1 $att2\n";
    close $fh;
}

print "done\n";
使用算法::循环qw(嵌套循环);
使用警告;
打开(文件,app_txt);
($app=());
关闭(文件);
我的@app=split/,/,$app;
打开(文件,att1_txt);
($att1=());
关闭(文件);
my@att1=拆分/,/,$att1;
打开(文件,att2_txt);
($att2=());
关闭(文件);
my@att2=拆分/,/,$att2;
my$iter=NestedLoops([\@app,\@att1,\@att2]);
而(我的($app,$att1,$att2)=$iter->()){
打开(我的$fh,“>”,“测试”);
打印$fh“$app$att1$att2\n”;
收盘价$fh;
}
打印“完成”\n;

我通过作弊解决了上一个格式化问题。我刚刚编辑了源文件并删除了所有\r和\n

最终代码:

use Algorithm::Loops qw( NestedLoops );
use warnings;

open (FILE, app_txt);
 ($app = (<FILE>));
close (FILE);
my @app = split /,/, $app;

open (FILE, att1_txt);
 ($att1 = (<FILE>));
close (FILE);
my @att1 = split /,/, $att1;

open (FILE, att2_txt);
 ($att2 = (<FILE>));
close (FILE);
my @att2= split /,/, $att2;

my $iter = NestedLoops([ \@app, \@att1, \@att2 ]);
while ( my ($app, $att1, $att2) = $iter->() ) {
    open (my $fh, '>', 'test');
    print  $fh "$app $att1 $att2\n";
    close $fh;
}

print "done\n";
使用算法::循环qw(嵌套循环);
使用警告;
打开(文件,app_txt);
($app=());
关闭(文件);
我的@app=split/,/,$app;
打开(文件,att1_txt);
($att1=());
关闭(文件);
my@att1=拆分/,/,$att1;
打开(文件,att2_txt);
($att2=());
关闭(文件);
my@att2=拆分/,/,$att2;
my$iter=NestedLoops([\@app,\@att1,\@att2]);
而(我的($app,$att1,$att2)=$iter->()){
打开(我的$fh,“>”,“测试”);
打印$fh“$app$att1$att2\n”;
收盘价$fh;
}
打印“完成”\n;

我就是为了这个目的而写的。

我就是为了这个目的而写的。

请展示你想要的1,2,3/4,5,6/7,8,9示例的输出,只是为了澄清你想要的是什么“我正试图使用List::Permutor打印组合”
List::Permutor
用于生成置换。如果您想要组合,那么您需要一个不同的模块,也许您从“为了清晰起见,让我们假设File1包含…”开始就做得很好,但是您再也不会引用该数据,因此没有清晰性!请解释您需要的简化数据;然后,我们将能够帮助您展示您想要的1,2,3/4,5,6/7,8,9示例的输出,请澄清您想要的内容“我正在尝试使用List::Permutor打印组合”
List::Permutor
用于生成置换。如果您想要组合,那么您需要一个不同的模块,也许您从“为了清晰起见,让我们假设File1包含…”开始就做得很好,但是您再也不会引用该数据,因此没有清晰性!请解释您需要的简化数据;那么我们就可以帮助你了,难道没有492960种方法可以排列80个项目吗?ie 80*79*78。如果他有3个文件“每个文件大约有80多个项目”,那么不是有240个项目被排列吗?@Marty,没有,有P(80,80)=80!=80*79*78*77*76*.*1=超过7e118种方式排列80个项目。//他没有说每个文件都有80个条目但是你提出了第三种解释,我已经在我的答案中添加了这种解释。(尽管根据这种解释,当每个文件中有80个项目时,会有P(80,1)*P(80,1)*P(80,1)=80*80*80=512000个结果,而不是492960个)。出于某种原因,我把重点放在有3个文件上,并将其与从收藏中选择3个项目混为一谈。不管怎样,我认为提问者不会很快把它们全部打印出来谢谢大家,自从我昨晚发布这个问题以来,大家都发表了评论。为了进一步澄清,我希望数组保持这种顺序(我刚刚意识到我没有spe)