Oh wise ones,
Lets suppose we have a table consisting of non unique rows which i want to collapse and sum up:
string1 10
string2 30
string1 0
string4 39
string9 345
etc.
I usually use hashes to keep count with a loop:
my $rh_count = { } ;
if ( exists $rh_count->{$string} ) {
$rh_count->{$string}++ ; #increment
} else {
$rh_count->{$string}=1 ; #set to one
}
What i am looking for, is there a more 'elegant' way of doing this. I know this is a minor detail but I was wondering if there is any other way of doing so. Consider the number of different strings not to be known in advance so pre-declaring the hash with the elements inside just to
increment them would not work.
Bless you
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.