in reply to using hashes to count

To continue from where mortiz left off, summing up the values is easy too.
my %rh_count; $rh_count{$string} += $value;

Replies are listed 'Best First'.
Re^2: using hashes to count
by spiros (Beadle) on Jun 14, 2007 at 23:01 UTC
    Thank you both of your replies. Yes i am just counting up the occurrences of the strings. What i am worried is, without the 'exists' check, $rh_count->{$keyword}++ would give a warning since it would be undef (provided that it wouldnt have been used before)

      Usual way:

      my @strings = qw/ a a b c a b c d e a b c a b c d /; my %count; foreach ( @strings ) { $count{$_} ||= 0; $count{$_}++; } # Then, if you want to sort the results by value: foreach ( sort { $count{$a} <=> $count{$b} } keys %count ) { print "$_ => $count{$_}\n"; }
        The $count{$_} ||= 0; is useless. ++ doesn't give undefined warnings.
        Excellent, thank you very much.