List Info

Thread: Re: Alphabetize a word




Re: Alphabetize a word
country flaguser name
United States
2007-09-07 05:22:09

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">merlynstonehenge.com (Randal L. Schwartz)
09/05/2007 12:36 PM

To
Ryan J Nauman < RJNauman%40uss.com">RJNaumanuss.com&gt;
cc
perl-beginner%40yahoogroups.com">perl-beginneryahoogroups.com
Subject
Re: [PBML] Alphabetize a word

>>>;>> "Ryan" == Ryan J Nauman < RJNauman%40uss.com">RJNaumanuss.com&gt; 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&quot;;
}

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">merlynstonehenge.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]

__._,_.___
.

__,_._,___
Odd hash behavior...
country flaguser name
United States
2007-09-07 06:15:51

I have the following code for pushing a new value onto my hash:

my %buckets;
push({ $buckets{&quot;KEYNAME"} }, "new value";);
my keys = keys %buckets;
my values = values %buckets;
print %buckets;
print "nkeys[0] => values[0]&quot;;

This code yields the following output...

KEYNAMEARRAY(0x15d702c)
KEYNAME => ARRAY(0x15d702c)

Why won't it print "new value"; ???

I'm running perl, v5.8.8 build 822 from ActiveState.

[Non-text portions of this message have been removed]

__._,_.___
.

__,_._,___
[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )