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