spiros has asked for the wisdom of the Perl Monks concerning the following question:
etc. I usually use hashes to keep count with a loop:string1 10 string2 30 string1 0 string4 39 string9 345
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 youmy $rh_count = { } ; if ( exists $rh_count->{$string} ) { $rh_count->{$string}++ ; #increment } else { $rh_count->{$string}=1 ; #set to one }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: using hashes to count
by moritz (Cardinal) on Jun 14, 2007 at 22:41 UTC | |
|
Re: using hashes to count
by FunkyMonk (Bishop) on Jun 14, 2007 at 22:45 UTC | |
|
Re: using hashes to count
by ikegami (Patriarch) on Jun 14, 2007 at 22:48 UTC | |
by spiros (Beadle) on Jun 14, 2007 at 23:01 UTC | |
by Joost (Canon) on Jun 14, 2007 at 23:13 UTC | |
by nferraz (Monk) on Jun 15, 2007 at 09:43 UTC | |
by ikegami (Patriarch) on Jun 15, 2007 at 13:16 UTC | |
by spiros (Beadle) on Jun 15, 2007 at 10:00 UTC |