Perl | Count the frequency of words in text

Counting frequency of all words of a string is a basic operation for any programming language. The frequency of each word of the text can be counted and stored in a hash for further use. In Perl, we can do this by firstly splitting the words of the string into an array. We use the function split / / which splits the string with ‘ ‘. However the blank spaces can be more than one in between two words, therefore /\s+/ is used. Here \s+ denotes one or more occurrence of ‘ ‘. Now we traverse the new array created by splitting of text into words. This time we increment the count of the word while traversing the array.

  • Example: To demonstrate Count the frequency of words in string
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Perl program for counting words in a string
      
    $actual_text = "GFG GeeksforGeeks GFG" ;
      
    # Creating words array by splitting the string
    @words= split / /, $actual_text;
       
    # Traversing the words array and 
    # increasing count of each word by 1
    foreach $word(@words
    {
        $count{$word}++;
    }
      
    # Printing the word and its actual count
    foreach $word (sort keys %count
    {
        print $word, " ", $count{$word}, "\n";
    }

    chevron_right

    
    

    Output:

    GFG 2
    GeeksforGeeks 1
    

Difference between /\s+/ and / /: The ‘\s+’ can be used for a delimiter with one or many space. However / / just separates words with 1 space. The following code represents the difference if the text has more than one space between two words.



  • Example: To demonstrate the difference between /\s+/ and / /
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

       
    # Perl program for counting words in a string using / / 
      
    # A text with two spaces rather than one
    $actual_text = "GeeksforGeeks  welcomes you to GeeksforGeeks portal" ;
      
    # splitting the word with / /
    @words= split / /, $actual_text;
      
    # Counting the occurrence of each word  
    foreach $word (@words)
    {
        $count{$word}++;
    }
      
    foreach $word (sort keys %count)
    {
        print $word, " ", $count{$word}, "\n";
    }

    chevron_right

    
    

    Output:

    1
    GeeksforGeeks 2
    portal 1
    to 1
    welcomes 1
    you 1
    

    Note: The extra ‘ ‘ is also counted as a word.

Using the command /\s+/ to split the words: Here space will not count as the separate word.

  • Example:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

       
    #Perl program for counting words in a string using /\s+/
      
    # Actual string with two spaces
    $actual_text = "GeeksforGeeks  welcomes you to GeeksforGeeks portal" ;
      
    #S plitting the text using /\s+/ command
    @words = split /\s+/, $actual_text;
       
    # counting the occurrence of each word  
    foreach $word (@words
    {
        $count{$word}++;
    }
      
    foreach $word (sort keys %count)
    {
        print $word, " ", $count{$word}, "\n";
    }

    chevron_right

    
    

    Output:

    GeeksforGeeks 2
    portal 1
    to 1
    welcomes 1
    you 1
    

    Note: The extra ‘ ‘ is not counted as a word.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.