Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > multimap equivalent in perl

Reply
Thread Tools

multimap equivalent in perl

 
 
Kapil Khosla
Guest
Posts: n/a
 
      05-05-2004
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
 
Reply With Quote
 
 
 
 
Jim Cochrane
Guest
Posts: n/a
 
      05-05-2004
In article <(E-Mail Removed)> , 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; http://www.velocityreviews.com/forums/(E-Mail Removed)
[When responding by email, include the term non-spam in the subject line to
get through my spam filter.]
 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      05-05-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Python's equivalent of C++ std::multimap Boogie El Aceitoso Python 2 03-05-2004 12:20 PM
Multimap Håvard Kverneland Java 1 02-10-2004 12:07 PM
std::multimap insertion order guarantees Tanguy Fautré C++ 13 10-06-2003 04:34 PM
Re: Find specific element of multimap? John Harrison C++ 1 08-14-2003 09:42 AM
Anyone use Multimap ? slumpy Computer Support 4 07-17-2003 07:02 PM



Advertisments