简体   繁体   中英

Perl syntax error with hashes

I'm new to Perl and the compiler is giving me a syntax error when I'm trying to use a hash. This is where the problem is:

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

and the error I'm getting is

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. 

To access a hash value, use the scalar sigil $ . Change:

        %wordcount{$word}++;

to:

        $wordcount{$word}++;

perldoc perldata

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM