counting word occurances

Discussion in 'Perl' started by Rodrick Brown, Jun 1, 2005.

  1. apple
    orange
    pear
    pear
    magno
    apple

    Given the following data can someone show me example code on how to count
    how many of each different fruit

    ie.

    output should be somehting like

    apple 2
    orange 1
    pear 1
    mango 1

    Note this isnt homework or anyting.
     
    Rodrick Brown, Jun 1, 2005
    #1
    1. Advertising

  2. *** post for FREE via your newsreader at post.newsfeed.com ***

    >>>>> "Rodrick" == Rodrick Brown <> writes:


    Rodrick> Note this isnt homework or anyting.

    Ding ding ding. Homework alert. :)

    Not to mention "unable to tell a dead group from a live one".

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <> <URL:http://www.stonehenge.com/merlyn/>
    Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
    See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!


    -----= Posted via Newsfeed.Com, Uncensored Usenet News =-----
    http://www.newsfeed.com - The #1 Newsgroup Service in the World!
    -----== 100,000 Groups! - 19 Servers! - Unlimited Download! =-----
     
    Randal L. Schwartz, Jun 2, 2005
    #2
    1. Advertising

  3. Rodrick Brown wrote:
    > apple
    > orange
    > pear
    > pear
    > magno
    > apple
    >
    > Given the following data can someone show me example code on how to
    > count how many of each different fruit


    Well, assuming @foo contains the list of fruit, then

    for (@foo) {
    $bar{$_}++;}

    will place the count in %bar.

    > output should be somehting like


    The rest is left as an excercise.

    jue
     
    Jürgen Exner, Jun 2, 2005
    #3
    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. =?Utf-8?B?SmltIEhlYXZleQ==?=

    Multiple Occurances Of Value In String

    =?Utf-8?B?SmltIEhlYXZleQ==?=, Jun 29, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    519
    Martin Marinov
    Jun 29, 2004
  2. Franz Steinhaeusler
    Replies:
    9
    Views:
    501
    Fredrik Lundh
    Dec 15, 2004
  3. John
    Replies:
    8
    Views:
    385
    Fredrik Lundh
    Mar 11, 2006
  4. Sandman
    Replies:
    7
    Views:
    211
    Anno Siegel
    Aug 3, 2004
  5. Rodrick Brown

    counting word occurances

    Rodrick Brown, Jun 3, 2005, in forum: Perl Misc
    Replies:
    22
    Views:
    293
Loading...

Share This Page