PresidentNick
PresidentNick

Reputation: 98

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. 

Upvotes: 1

Views: 478

Answers (1)

toolic
toolic

Reputation: 62037

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

        %wordcount{$word}++;

to:

        $wordcount{$word}++;

perldoc perldata

Upvotes: 7

Related Questions