in reply to count frequency of occurence....

%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.
RIP PCW It is as I've been saying!(Audio until 20090817)

Replies are listed 'Best First'.
Re^2: count frequency of occurence....
by wfsp (Abbot) on Nov 03, 2009 at 08:00 UTC
    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