FAQ 6.15 How can I print out a word-frequency or line-frequency summary?

Discussion in 'Perl Misc' started by PerlFAQ Server, Mar 26, 2011.

  1. This is an excerpt from the latest version perlfaq6.pod, which
    comes with the standard Perl distribution. These postings aim to
    reduce the number of repeated questions as well as allow the community
    to review and update the answers. The latest version of the complete
    perlfaq is at http://faq.perl.org .

    --------------------------------------------------------------------

    6.15: How can I print out a word-frequency or line-frequency summary?

    To do this, you have to parse out each word in the input stream. We'll
    pretend that by word you mean chunk of alphabetics, hyphens, or
    apostrophes, rather than the non-whitespace chunk idea of a word given
    in the previous question:

    while (<>) {
    while ( /(\b[^\W_\d][\w'-]+\b)/g ) { # misses "`sheep'"
    $seen{$1}++;
    }
    }

    while ( ($word, $count) = each %seen ) {
    print "$count $word\n";
    }

    If you wanted to do the same thing for lines, you wouldn't need a
    regular expression:

    while (<>) {
    $seen{$_}++;
    }

    while ( ($line, $count) = each %seen ) {
    print "$count $line";
    }

    If you want these output in a sorted order, see perlfaq4: "How do I sort
    a hash (optionally by value instead of key)?".



    --------------------------------------------------------------------

    The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
    are not necessarily experts in every domain where Perl might show up,
    so please include as much information as possible and relevant in any
    corrections. The perlfaq-workers also don't have access to every
    operating system or platform, so please include relevant details for
    corrections to examples that do not work on particular platforms.
    Working code is greatly appreciated.

    If you'd like to help maintain the perlfaq, see the details in
    perlfaq.pod.
     
    PerlFAQ Server, Mar 26, 2011
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin
    Replies:
    16
    Views:
    8,257
    Dale King
    Apr 19, 2005
  2. Gordon Odell

    Looking for a word frequency counter.

    Gordon Odell, Feb 15, 2006, in forum: HTML
    Replies:
    2
    Views:
    488
    Stefan B Rusynko
    Feb 16, 2006
  3. Frost
    Replies:
    8
    Views:
    519
    Vladimir S. Oka
    Feb 10, 2006
  4. x1
    Replies:
    9
    Views:
    322
    Rick DeNatale
    Oct 12, 2006
  5. PerlFAQ Server
    Replies:
    0
    Views:
    210
    PerlFAQ Server
    Feb 1, 2011
Loading...

Share This Page