具有哈希的Perl语法错误

时间:2014-02-05 01:56:25

标签: perl syntax

我是Perl的新手,当我尝试使用哈希时,编译器会给我一个语法错误。 这就是问题所在:

while (<>){
    @words_in_line = /[a-z](?:[a-z']*[a-z])?/ig;
    foreach $word (@words_in_line){
            %wordcount{$word}++;
    }
}

我得到的错误是

syntax error at ./wordfreq.pl line 11, near "%wordcount{"
syntax error at ./wordfreq.pl line 11, near "++;"
syntax error at ./wordfreq.pl line 13, near "}"
Execution of ./wordfreq.pl aborted due to compilation errors. 

1 个答案:

答案 0 :(得分:7)

要访问哈希值,请使用标量符号$。变化:

        %wordcount{$word}++;

为:

        $wordcount{$word}++;

perldoc perldata