multimap equivalent in perl

Discussion in 'Perl Misc' started by Kapil Khosla, May 5, 2004.

  1. Kapil Khosla

    Kapil Khosla Guest

    Hi,
    I am new to perl and am trying to count the number of instances of
    unique strings in a file. The strings are like

    s1\aaa s1\bbb s1\ccc s1\ddd s1\aaa s1\aaa s1\aaa s1\aaa s1\eee

    Currently, I have no good way to count unique strings in the file.
    This could be easily done using the multimap functionality in C++.

    I read about associative arrays but they seem to be more like unique
    key / value combination. Would you know how best could I count the
    number of unique strings in a file. I am not looking for code, just a
    pointer in the right directin.

    Thanks for your help,
    Kapil
     
    Kapil Khosla, May 5, 2004
    #1
    1. Advertising

  2. Kapil Khosla

    Jim Cochrane Guest

    In article <>, Kapil Khosla wrote:
    > Hi,
    > I am new to perl and am trying to count the number of instances of
    > unique strings in a file. The strings are like
    >
    > s1\aaa s1\bbb s1\ccc s1\ddd s1\aaa s1\aaa s1\aaa s1\aaa s1\eee
    >
    > Currently, I have no good way to count unique strings in the file.
    > This could be easily done using the multimap functionality in C++.
    >
    > I read about associative arrays but they seem to be more like unique
    > key / value combination. Would you know how best could I count the
    > number of unique strings in a file. I am not looking for code, just a
    > pointer in the right directin.
    >
    > Thanks for your help,
    > Kapil


    Something like:

    my %word_table = ();

    for (all_words_in_file()) {
    ++$word_table{$_};
    }

    sub all_words_in_file {
    # To be implemented
    }

    END {
    for my $key (keys %word_table) {
    print "$key occurs $word_table{$key} times.\n";
    }
    }

    # Warning - the above is not tested.

    --
    Jim Cochrane;
    [When responding by email, include the term non-spam in the subject line to
    get through my spam filter.]
     
    Jim Cochrane, May 5, 2004
    #2
    1. Advertising

  3. Kapil Khosla

    Paul Lalli Guest

    On Wed, 4 May 2004, Kapil Khosla wrote:

    > Hi,
    > I am new to perl and am trying to count the number of instances of
    > unique strings in a file. The strings are like
    >
    > s1\aaa s1\bbb s1\ccc s1\ddd s1\aaa s1\aaa s1\aaa s1\aaa s1\eee
    >
    > Currently, I have no good way to count unique strings in the file.
    > This could be easily done using the multimap functionality in C++.
    >
    > I read about associative arrays but they seem to be more like unique
    > key / value combination. Would you know how best could I count the
    > number of unique strings in a file. I am not looking for code, just a
    > pointer in the right directin.
    >
    > Thanks for your help,
    > Kapil


    If you want a pointer to the right information, the FAQ is generally a
    good place to start. In this case, the appropriate FAQ can be found by
    typing
    perldoc -q word-frequency
    into your shell

    And yes, associative arrays are indeed unique key/value combinations. In
    your case, the keys would be the unique strings, and the values are the
    number of times those strings have appeared.

    Hope this helps,
    Paul Lalli
     
    Paul Lalli, May 5, 2004
    #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. Håvard Kverneland

    Multimap

    Håvard Kverneland, Feb 10, 2004, in forum: Java
    Replies:
    1
    Views:
    665
    Håvard Kverneland
    Feb 10, 2004
  2. John Harrison
    Replies:
    1
    Views:
    592
    Dave O'Hearn
    Aug 14, 2003
  3. Tanguy Fautré

    std::multimap insertion order guarantees

    Tanguy Fautré, Oct 5, 2003, in forum: C++
    Replies:
    13
    Views:
    825
    David B. Held
    Oct 6, 2003
  4. al

    map and multimap

    al, Jan 2, 2004, in forum: C++
    Replies:
    5
    Views:
    479
    Nick Hounsome
    Jan 3, 2004
  5. Boogie El Aceitoso

    Python's equivalent of C++ std::multimap

    Boogie El Aceitoso, Mar 5, 2004, in forum: Python
    Replies:
    2
    Views:
    1,959
    Edward C. Jones
    Mar 5, 2004
Loading...

Share This Page