Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.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
File 在perl中,如何生成文件中包含的所有可能的数字组合?_File_Perl_Combinations_Args - Fatal编程技术网

File 在perl中,如何生成文件中包含的所有可能的数字组合?

File 在perl中,如何生成文件中包含的所有可能的数字组合?,file,perl,combinations,args,File,Perl,Combinations,Args,我在本地找到了下面的perl代码,它可以计算所有可能的字符或数字组合,但您需要使用qw函数my@strings=[qw(12345678911113)]提供它们,我需要从文件中读取这些数字(1 2 3 4 5 6 7 8 9 10 11 12 13),并将它们传递到@strings数组,或者通过Perl行命令参数将数字传递到提到的@strings数组 我已经阅读了有关qw()的所有信息,但在阅读Perl行命令参数文件时,我没有找到使用它的方法,因此您能否给出一些建议以解决此问题 现在提供的输出是

我在本地找到了下面的perl代码,它可以计算所有可能的字符或数字组合,但您需要使用qw函数
my@strings=[qw(12345678911113)]提供它们,我需要从文件中读取这些数字(1 2 3 4 5 6 7 8 9 10 11 12 13),并将它们传递到
@strings
数组,或者通过Perl行命令参数将数字传递到提到的
@strings
数组

我已经阅读了有关
qw()
的所有信息,但在阅读Perl行命令参数文件时,我没有找到使用它的方法,因此您能否给出一些建议以解决此问题

现在提供的输出是:

1 2 3 4 5
1 2 3 4 6
1 2 3 4 7 ...
代码:

使用严格;
使用警告;
#my$strings=[qw(AAA BBB CCC DDD EEE)];
#我的$strings=[qw(123456789101113)];
my@strings=[qw(123456789101113)];
联合收割机;
为联合收割机@字符串打印“@$\n”,5;
联合收割机{
我的($list,$n)=@;
如果$n>@$list,则禁用“列表成员不足”;

如果$n此代码段初始化文件中的数字数组(
@numbers
),则返回map[$\u],@$list:

use strict;
use warnings;

my $filename = "data.txt";
my @numbers; 

open my $fh, "<", $filename or die "Cannot open $filename";

# Read each line into $line
while( my $line = <$fh> ) {
    # Extract each number present at $line
    while( $line =~ /(\d+)/g ) {
        # If found, add the number to @numbers
        push @numbers, $1;
    }
}

close $fh;

如果每行中有多个数字,则无所谓。如果事实上,它们可以位于带有分隔符的单行中(数字以外的东西,如空格)

这段代码初始化文件中的数字数组(
@numbers
):

use strict;
use warnings;

my $filename = "data.txt";
my @numbers; 

open my $fh, "<", $filename or die "Cannot open $filename";

# Read each line into $line
while( my $line = <$fh> ) {
    # Extract each number present at $line
    while( $line =~ /(\d+)/g ) {
        # If found, add the number to @numbers
        push @numbers, $1;
    }
}

close $fh;

如果每行中有多个数字,则无所谓。如果事实上,它们可以在一行中使用分隔符(数字以外的东西,如空格)

首先,这是不对的:

my@strings=[qw(123456789101113)];
它使用单个元素创建一个数组,即对另一个数组的引用

你想要

my@strings=qw(123456789101123);
组合\@字符串,5;

my$strings=[qw(123456789101113)];
组合$strings,5;

qw(…)
相当于
split',q(…)
,其中
q(…)
只是带有不同分隔符的
“…”

这意味着

my@strings=qw(123456789101123);
组合\@字符串,5;
相当于

my@strings=split(“”,'123456789101113');
组合\@字符串,5;
但当然,我们不需要使用单引号来构造传递给
split
的字符串;我们可以使用传递通过读取文件创建的字符串

因此,从文件中读取相当于

my@strings=qw(123456789101123);
组合\@字符串,5;
会是

my$line=;
chomp($line);
我的@strings=split(“”,$line);
组合\@字符串,5;

(实际上没有必要使用
chomp
,因为
split''
忽略了尾随空格。)

首先,这是不对的:

my@strings=[qw(123456789101113)];
它使用单个元素创建一个数组,即对另一个数组的引用

你想要

my@strings=qw(123456789101123);
组合\@字符串,5;

my$strings=[qw(123456789101113)];
组合$strings,5;

qw(…)
相当于
split',q(…)
,其中
q(…)
只是带有不同分隔符的
“…”

这意味着

my@strings=qw(123456789101123);
组合\@字符串,5;
相当于

my@strings=split(“”,'123456789101113');
组合\@字符串,5;
但当然,我们不需要使用单引号来构造传递给
split
的字符串;我们可以使用传递通过读取文件创建的字符串

因此,从文件中读取相当于

my@strings=qw(123456789101123);
组合\@字符串,5;
会是

my$line=;
chomp($line);
我的@strings=split(“”,$line);
组合\@字符串,5;
(实际上没有必要使用
chomp
,因为
split'
忽略尾随空格。)

您可以使用以下方法执行相同操作:

输出

1 2
1 3
1 4
1 5
1 6
2 3
2 4
2 5
2 6
3 4
3 5
3 6
4 5
4 6
5 6
您可以使用以下方法执行相同操作:

输出

1 2
1 3
1 4
1 5
1 6
2 3
2 4
2 5
2 6
3 4
3 5
3 6
4 5
4 6
5 6

如果每行有一个数字,请检查
chomp()
,如果有一行包含所有数字,请同时检查
split()
。如果每行有一个数字,请检查
chomp()
,如果有一行包含所有数字,请同时检查
split())
。对
combine
的相应调用将是
combine\@numbers,5
,对
combine
的相应调用将是
combine\@numbers,5
是的,但问题是如何从文件中的
123456789101123创建正确的数组。也许这应该是一个注释。…是的,但问题是如何从文件中的
1 2 3 4 5 6 7 8 9 10 11 12 13
创建正确的数组。也许这应该是一个注释…ikegami,你的解决方案很好,可以处理\@string,但我想知道你为什么对它使用反冲?我还测试了$strings,我得到了一个错误,比如:不能使用string(“13”)作为数组引用,而“严格引用”在pgms\test2.pl中使用的第28行是生成的。这是因为
combine
需要对数组的引用。您不能将数组传递给sub,只能传递标量。我们可以通过将引用传递给数组来绕过该限制。///我不知道您测试了什么,但这不是我发布的代码。您没有将数组的引用分配给
$strings
.ikegami,你的解决方案很好,可以处理\@string,但我想知道你为什么要在它上使用反冲?我还测试了$strings,我得到了一个错误,比如:不能使用string(“13”)作为数组引用
1 2
1 3
1 4
1 5
1 6
2 3
2 4
2 5
2 6
3 4
3 5
3 6
4 5
4 6
5 6