寻找多处 0 的问题?

寻找多处 0 的问题?

我有这样一个数组:
0 1 2 4 0 0 0 0 9 2 1 0 0 4 7 2 0 0 0 1 6 5 0 1 8
我想出现几次0区,每个0区 有多少个0?
上面那个数组应该是:
出现5次0区,
每个0区的0个数为:1 4 2 3 1
谢谢!
應該和正規表示式有關
待高手解答=.=
0 1 2 4 0 0 0 0 9 2 1 0 0 4 7 2 0 0 0 1 6 5 0 1 8 是一个数组,不是一个字符串,
唧:
@array=(0,1,2,4,0,0,0,0,9,2,1,0,0,4,7,2,0,0,0,1,6,5,0,1,;
#!/usr/bin/perl


my $string = "0 1 2 4 0 0 0 0 9 2 1 0 0 4 7 2 0 0 0 1 6 5 0 1 8";
$string =~s/\s+//g;
$string =~s/[1-9]{1,}/|/g;
my @array = split(/\|/,$string);

print "Total P:".@array."\n";

foreach (@array)
{
        print $_."\thas\t".length($_)."\n";
}


根据实际情况再修改一下吧.
数组的话就稍微该一下.

#!/usr/bin/perl


my @string = (0,1,2,4,0,0,0,0,9,2,1,0,0,4,7,2,0,0,0,1,6,5,0,1,8);
my $string;
map {$string .= $_} @string;
$string =~s/\s+//g;
$string =~s/[1-9]{1,}/|/g;
my @array = split(/\|/,$string);

print "Total P:".@array."\n";

foreach (@array)
{
        print $_."\thas\t".length($_)."\n";
}
# 走算法路线就遍历数组.
# 走Perl路线就转换成字符串.

my @array = qw/0 1 2 4 0 0 0 0 9 2 1 0 0 4 7 2 0 0 0 1 6 5 0 1 8/;
my $str   = join('', @array);
my @count;

for ($str =~ /0+/g) {
#Or: for (split(/[1-9]+/, $str)) {
    push @count, length $_;
}

print join(' ', @count);    # 1 4 2 3 1




QUOTE:
原帖由 Lonki 于 2007-11-2 01:26 发表
# 走算法路线就遍历数组.
# 走Perl路线就转换成字符串.

my @array = qw/0 1 2 4 0 0 0 0 9 2 1 0 0 4 7 2 0 0 0 1 6 5 0 1 8/;
my $str   = join('', @array);
my @count;

for ($str =~ /0+/g) {
#Or ...

这个方法好
想法不错,值得称赞


QUOTE:
原帖由 Lonki 于 2007-11-2 01:26 发表
# 走算法路线就遍历数组.
# 走Perl路线就转换成字符串.

my @array = qw/0 1 2 4 0 0 0 0 9 2 1 0 0 4 7 2 0 0 0 1 6 5 0 1 8/;
my $str   = join('', @array);
my @count;

for ($str =~ /0+/g) {
#Or ...