Can someone please help me understand what is being explained here? I
thought I understood it after looking at it a second time but the part
that confuses me now is the declare of "my %buckets" but we never use it,
we use a scalar called $bucket. So, now I am completely lost.
If someone could break it down, it'd be much appreciated.
------------------------
Ryan
merlyn%40stonehenge.com">merlyn
stonehenge.com (Randal L. Schwartz)
09/05/2007 12:36 PM
To
Ryan J Nauman < RJNauman%40uss.com">RJNauman
uss.com>
cc
perl-beginner%40yahoogroups.com">perl-beginner
yahoogroups.com
Subject
Re: [PBML] Alphabetize a word
>>>>> "Ryan" == Ryan J Nauman < RJNauman%40uss.com">RJNauman
uss.com> writes:
Ryan> Ah, thanks. Neat little trick. Well I finished my first complete
working
Ryan> version of the code and is a bit sluggish. Though this is expected
since
Ryan> the input file contains about 170,000 words stuffed into my
WORDS
array.
Ryan> Wondering if you guys know any tricks to speed things up anywhere?
Here
Ryan> is a link to my source code: http://pastie.textmate.org/94246
Ooof, yeah. You're doing an exponential matching O(n squared)
instead of just stashing everything according to its anagram.
You need to use a "bucket" approach. As you compute each anagram,
dump the original word into a bucket keyed by anagram, using a hash
of arrayrefs...
my %buckets;
for my $word (
words) {
my $alphaword = alphabetize($word);
push
{$bucket{$alphaword}}, $word;
}
Now it's simply a matter of dumping each bucket:
for my $alphaword (sort keys %bucket) {
my $aref = $bucket{$alphaword};
print "
$arefn";
}
That will be infinitely faster when you get above a few hundred words.
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777
0095
< merlyn%40stonehenge.com">merlyn
stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl
training!
[Non-text portions of this message have been removed]
.