how to generate an associative array and count frequency?

how to generate an associative array and count frequency?

am 21.12.2007 01:57:11 von MT

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?

Re: how to generate an associative array and count frequency?

am 26.12.2007 00:13:49 von Joost Diepenmaat

"a" writes:

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

Re: how to generate an associative array and count frequency?

am 26.12.2007 01:59:09 von krahnj

On Wed, 26 Dec 2007 00:13:49 +0100
Joost Diepenmaat wrote:

> "a" writes:
>
> > 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;

Shouldn't that be:

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



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall

Re: how to generate an associative array and count frequency?

am 26.12.2007 02:15:26 von Joost Diepenmaat

"John W. Krahn" writes:
> Joost Diepenmaat wrote:
>> $wordcount{$i}++ while /(\w+)/g;
>
> Shouldn't that be:
>
> $wordcount{$1}++ while /(\w+)/g;

Yes it should.

Thanks for catching that.

Joost.

Re: how to generate an associative array and count frequency?

am 26.12.2007 05:22:30 von jurgenex

Joost Diepenmaat wrote:

>"a" writes:
>
>> 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?

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