[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Omaha.pm] Word frequency counter



BEFORE:

 

  1. use feature ':5.10';
  2.  
  3. #<---------- Word frequency counter ---------->
  4.  
  5. # ARRAY
  6.  
  7. my @words = ("We", "She", "We", "He", "We", "It", "He", "We", "She", "He");
  8. my $word_count = @words; # Count the total of words
  9.  
  10. my $i = 0;
  11. my $we_counter = 0;
  12. my $she_counter = 0;
  13. my $he_counter = 0;
  14. my $it_counter = 0;
  15.  
  16. while ($i < $word_count){
  17.  
  18. if ($word[$i] eq 'We') {
  19.     $we_counter++;
  20.     $i++;
  21. }
  22.  
  23. elsif ($word[$i] eq 'She') {
  24.     $she_counter++;
  25.     $i++;
  26. }
  27.  
  28. elsif ($word[$i] eq 'He') {
  29.     $he_counter++;
  30.     $i++;
  31. }
  32.  
  33. else {
  34.     $it_counter++;
  35.     $i++;
  36. }    
  37. }
  38.  
  39.  
  40. my %report_list = ("We" => $we_counter, "She" => $she_counter, "He" => $he_counter, "It" => $it_counter);
  41.  
  42.  
  43. say "\n___________________________________________\n#\n#";
  44. say "#  Word frequency counter\n#";
  45.  
  46. say "#\n#  There where a total of $word_count words!";
  47.  
  48. say "#\n#  We : $report_list{We}";
  49. say "#  She: $report_list{She}";
  50. say "#  He : $report_list{He}";
  51. say "#  It : $report_list{It}";
  52. say "#\n#    $we_counter $she_counter $he_counter $it_counter";
  53. say "#\n#__________________________________________\n\n";

 

 

AFTER:

 

   my %counts; $counts{$_}++ for @words;

 

 

 

:)

 

j