in reply to Re^2: using hashes to count
in thread using hashes to count

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"; }

Replies are listed 'Best First'.
Re^4: using hashes to count
by ikegami (Patriarch) on Jun 15, 2007 at 13:16 UTC
    The $count{$_} ||= 0; is useless. ++ doesn't give undefined warnings.
Re^4: using hashes to count
by spiros (Beadle) on Jun 15, 2007 at 10:00 UTC
    Excellent, thank you very much.