Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
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 contemplating the Monastery: (3)
As of 2024-04-24 03:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found