multimap equivalent in perl

K

Kapil Khosla

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
 
J

Jim Cochrane

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.
 
P

Paul Lalli

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top