我是Perl的新手,当我尝试使用哈希时,编译器给我一个语法错误.这就是问题所在:
while (<>){
@words_in_line = /[a-z](?:[a-z']*[a-z])?/ig;
foreach $word (@words_in_line){
%wordcount{$word}++;
}
}
Run Code Online (Sandbox Code Playgroud)
而我得到的错误是
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.
Run Code Online (Sandbox Code Playgroud)
要访问哈希值,请使用标量符号$.更改:
%wordcount{$word}++;
Run Code Online (Sandbox Code Playgroud)
至:
$wordcount{$word}++;
Run Code Online (Sandbox Code Playgroud)