Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.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
String 在Perl中拆分数字字符串_String_Perl_Split - Fatal编程技术网

String 在Perl中拆分数字字符串

String 在Perl中拆分数字字符串,string,perl,split,String,Perl,Split,我有一个数字字符串: "13245988" 我想在连续数字之前和之后拆分 预期产出为: 1 32 45 988 以下是我尝试过的: #!/usr/bin/perl use strict; use warnings; my $a="132459"; my @b=split("",$a); my $k=0; my @c=(); for(my $i=0; $i<=@b; $i++) { my $j=$b[$i]+1; if($b[$i] == $j) { $

我有一个数字字符串:

"13245988"
我想在连续数字之前和之后拆分

预期产出为:

1
32
45
988
以下是我尝试过的:

#!/usr/bin/perl
use strict;
use warnings;

my $a="132459";
my @b=split("",$a);
my $k=0;
my @c=();
for(my $i=0; $i<=@b; $i++) {
    my $j=$b[$i]+1;
    if($b[$i] == $j) {
        $c[$k].=$b[$i];
    } else {
        $k++;
        $c[$k]=$b[$i];
        $k++;
    }
}
foreach my $z (@c) {
    print "$z\n";
}

根据澄清的问题进行编辑。像这样的方法应该会奏效:

#!/usr/bin/perl
use strict;
use warnings;

my $a = "13245988";
my @b = split("",$a);

my @c = ();
push @c, shift @b; # Put first number into result.

for my $num (@b) { # Loop through remaining numbers.

    my $last = $c[$#c] % 10; # Get the last digit of the last entry.

    if(( $num <= $last+1) && ($num >= $last-1)) {
        # This number is within 1 of the last one
        $c[$#c] .= $num; # Append this one to it
    } else {
        push @c, $num; # Non-consecutive, add a new entry;
    }
}

foreach my $z (@c) {
    print "$z\n";
}

连续数字?我想这就是连续整数对的意思,比如32和45,而不是13。那你为什么不在98年后分手呢?哦,你试过什么了?琼,你猜对了。我尝试了下面的代码。但我得到的结果不同。严格使用;使用警告;我的$a=132459;我的@b=拆分,$a;我的$k=0;我的@c=;formy$i=0$iJean,很抱歉预期输出中出现错误。预期产出为13245988。这并不能回答这个问题。若要评论或要求作者澄清,请在其帖子下方留下评论。@dgw我根据澄清的问题进行了编辑,希望现在可以。嗨,Rob,代码运行良好。我不理解这部分代码my$last=$c[$c]%10;。你能详细说明一下吗?希望你不介意。非常感谢。@Iam,%是模运算符:my$c=$a%$b;将$a除以$b,并将剩余部分分配给$c。你可以在man perlop中找到更多细节。
1
32
45
988