cthar has asked for the wisdom of the Perl Monks concerning the following question:

I have the following hash table

word occurrence a 2 b 4 c 2 d 1 e 1 f 2 g 4 h 3

From the above table.. i want to count the frequencies as follows

Freq.of occurrence freq_count 0 0 1 2 2 3 3 1 4 2 5 0

I did something like this:

if(exists($count{$word})){ $occurence = $count{$word}; freq_count++; }
How to go about it? Your guidances will be much more appreciated.. Thanks..

Replies are listed 'Best First'.
Re: count frequency of occurence....
by BrowserUk (Patriarch) on Nov 03, 2009 at 06:44 UTC

    %h = qw[ a 2 b 4 c 2 d 1 e 1 f 2 g 4 h 3 ];; pp \%h;; { a => 2, b => 4, c => 2, d => 1, e => 1, f => 2, g => 4, h => 3 } ++$f{ $_ } for values %h;; pp \%f;; { 1 => 2, 2 => 3, 3 => 1, 4 => 2 }

    If you need the zero counts also, the initialise the frequency hash first:

    $f2{ $_ } = 0 for 0 .. 5;; ++$f2{ $_ } for values %h;; pp \%f2;; { "0" => 0, 1 => 2, 2 => 3, 3 => 1, 4 => 2, 5 => 0 }

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      He's drawn the short straw. He calms himself, takes a deep breath and walks up to the mark.

      Ok, what's the pp and the ;; all about?

        pp is an export of Data::Dump. Much like Dumper from Data::Dumper, but with better formatting.

        The double semicolons are simply a device I use in my REPL to indicate that the input should be evaluated now. It allows me to enter multiple statements and multi-line snippets and have them executed as a single snippet:

        [0] Perl> for my $i ( 1 .. 10 ) { print $i; if( ( $i % 2 ) == 0 ) { print "$i is even"; } };; 1 2 2 is even 3 4 4 is even 5 6 6 is even 7 8 8 is even 9 10 10 is even [0] Perl>

        Thereby allowing me to try out more extensive pieces of code without having to stick it all on one line.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        I must have drawn the longer straw, but I'm still curious. I even glanced over the latest perldocs to see if maybe I missed something recent. Perhaps I still have, but didn't find what I was looking for on first run-through of the docs.


        Dave

Re: count frequency of occurence....
by Marshall (Canon) on Nov 03, 2009 at 08:59 UTC
    The hard bit about this appears to be filling in the "gaps" in the histogram. If the histogram was huge, then I suppose that a hash table would be best, filling in the "gaps" on the output. But in this case an array representation appears to be quite good. To find the maximum "peg" value, a numeric sort of the values and taking the last one in that list appears to me to be a good way to go. I think that jwkrahn was very close. Update: Well I suppose it is also possible that I've made the problem more complex than it needed to be!

    #!/usr/bin/perl -w use strict; my %hash = qw ( a 2 b 4 c 2 d 1 e 1 f 2 g 4 h 3 i 8 j 10 k 10 ); my @values = sort{$a<=>$b}values(%hash); my $max = (@values)[-1]; my @pegs = (0) x ($max); for my $value ( @values ) { $pegs[ $value ]++; } for my $peg_num (0..@pegs-1) { print "$peg_num \t=> $pegs[$peg_num]\n"; } __END__ Prints: 0 => 0 1 => 2 2 => 3 3 => 1 4 => 2 5 => 0 6 => 0 7 => 0 8 => 1 9 => 0 10 => 2
Re: count frequency of occurence....
by jwkrahn (Abbot) on Nov 03, 2009 at 06:41 UTC
    my @freq_count = ( 0 ) x values %count; for ( values %count ) { $freq_count[ $_ ]++; }