in reply to Speed up my code
It seems to me that a simple hash would do nicely. Perl has many shortcuts to simplify this sort of thing, for example:
You don’t have to worry if the string is already in the hash; you don’t have to initialize the bucket to zero. If the bucket isn’t in there, it is automagically initialized with the value of '1.' If it is, it’s incremented. It Just Works.™my $foo = "bar"; my $counts = {}; $$counts{$foo}++; // or $counts->{$foo}++; // yields: "$$counts{"bar"} == 1
There are many ways to get the final tallies out of the structure, depending on your needs.
This straight-ahead strategy works excellently for any data volume that can be reasonably expected to fit entirely in memory without incurring page-faults. (Which, given the beefy size of computers these days, is a pretty safe bet.)
The original choice of Perl was that the word was an acronym for P(ractical | ragmatic) Extraction and Reporting Language, otherwise known as The Swiss Army® Knife. And this is one of the reasons why. Text-handling tasks, that lots of folks have to do lots of ... those bread-and-butter tasks ... are easy to code and ruggedly implemented.
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Speed up my code
by AnomalousMonk (Archbishop) on Dec 15, 2011 at 02:59 UTC |