how to generate an associative array and count frequency?

A

a

to study the frequency of words composed of {a,b,c,d,e}, how can i tell perl
program to generate the associative array of (aaaaa, aaaab, ..., caaaa, ...,
eeeee) for me instead of declaring it explicitly?

Then instead of counting the occurrence of aaaaa but the co-existence, say,
aaaaabcdea or aaaaaaaaab, how can i combine the advantage of regular
expression with this associative array?
 
J

Joost Diepenmaat

a said:
to study the frequency of words composed of {a,b,c,d,e}, how can i tell perl
program to generate the associative array of (aaaaa, aaaab, ..., caaaa, ...,
eeeee) for me instead of declaring it explicitly?

Why would you want to? Just assume that any key that doesn't exist
corresponds to a word-count of 0.
Then instead of counting the occurrence of aaaaa but the co-existence, say,
aaaaabcdea or aaaaaaaaab, how can i combine the advantage of regular
expression with this associative array?

$wordcount{$i}++ while /(\w+)/g;

Or something similar should work.

HTH,
Joost.
 
J

John W. Krahn

Why would you want to? Just assume that any key that doesn't exist
corresponds to a word-count of 0.


$wordcount{$i}++ while /(\w+)/g;

Shouldn't that be:

$wordcount{$1}++ while /(\w+)/g;



John
 
J

Jürgen Exner

The set of those keys is called a permutation with repetition and a quick
search on CPAN finds several modules.
Why would you want to? Just assume that any key that doesn't exist
corresponds to a word-count of 0.

This of course is a very good suggestion, in particular considering that
Perl has auto-vivication.

jue
 

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

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top