高手看一下,这个程序错哪了?

高手看一下,这个程序错哪了?

#!/usr/bin/perl -w

use strict;
while(<>){
        foreach(split){
                my $total++;
                next if/\W/;
                my $valid++;
                my $count{$_}++;
        }
}
print "total things=$total,valid words=$valid\n";
foreach $word(sort keys %count){
        print "$word was seen $count{$word} time.\n";
}


执行的时候报这样的错误:

syntax error at ./danci line 8, near "$count{"
syntax error at ./danci line 8, near "++;"
syntax error at ./danci line 10, near "}"
Execution of ./danci aborted due to compilation errors.
貌似my $count{$_}++; 声明变量里面不能带++
变量范围有问题。
是啊 my $total++;这都没报错,那么还是该先声明散列,再++,
错在不该用my来修饰散列的成员?


QUOTE:
原帖由 dajun 于 2008-10-21 18:41 发表
是啊 my $total++;这都没报错,那么还是该先声明散列,再++,
错在不该用my来修饰散列的成员?

应该是这个
不声明的散列 好像不能直接调用成员吧
foreach ( ... ) {
    ...
    my $count{$_}++;
    ...
}

楼主显然还不太明白 my 的用法, 你这样写法在foreach{}循环外面是访问不了 %count 的, 循环一完 %count 这个变量就被销毁了
落叶季节,思念季节
改成这样应该就不会报错了:
#!/usr/bin/perl -w


use strict;

my $total;
my $valid;
my %count;

while(<>){
        foreach my $i ( split ){
                $total++;
                next if/\W/;
                $valid++;
                $count{$i}++;
        }
}
print "total things=$total,valid words=$valid\n";
foreach my $word(sort keys %count){
        print "$word was seen $count{$word} time.\n";
}
落叶季节,思念季节
谢谢楼上的各位。

foreach my $i ( split ){
                $total++;
                next if/\W/;
                $valid++;
                $count{$i}++;
        }


ls的foreach控制的是$i变量,而if控制的是默认变量$_。