I have just started learning perl a few weeks ago , I am facing a doubt in the below code . The program is supposed to give the count of number of times each word has appeared , but the output is not as expected.
When I give this input :
Perl
Programming
Perl
java
C++
Perl
Expected output :
C++ has appeared 1 times
java has appeared 1 times
Perl has appeared 3 times
Programming has appeared 1 times
Actual output :
C++
has appeared 1 times
Perl has appeared 1 times
Perl
has appeared 2 times
Programming
has appeared 1 times
java
has appeared 1 times
I am a total beginner in perl and programming too , kindly pardon me if I have asked any silly question , but do correct me so I can learn from my mistake , thanks !
#! /usr/bin/perl my (@words , %count , $words); chomp(@words = <STDIN>); foreach $words (@words){ $count{$words} += 1; } foreach $word (sort keys %count){ print "$word has appeared $count{$word} times\n"; }
In reply to Doubt in code - Beginner in perl by Perl_Programmer1992
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |