Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^3: using hashes to count

by nferraz (Monk)
on Jun 15, 2007 at 09:43 UTC ( [id://621426]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://621426]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (6)
As of 2024-04-26 09:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found