[Purdue-pm] fast table lookup of randomly associated items

Joe Kline gizmo at purdue.edu
Fri May 21 08:22:49 PDT 2010


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Rick Westerman wrote:
> 
<SNIP>
> 
>    Fastest?  I'd say a simple array lookup.  Could be quite memory intensive but, at least in 'C' and I presume Perl, array lookups are simple pointer additions.   Can't be any faster than that as long as you do not exceed physical memory boundries.
> 

Maybe fastest might be misnomer. I was hoping I could do something like:

my $random
my ($key) = grep { $_ ~~ $random } keys %table;

I stumbled across something like this at stackoverflow but I can't find
it now.

I was thinking that I could make the hash keys the range and then use
smart matching to figure out which key the random number is in the range of.

Using ranges for hash keys does weird things however. Let alone that
they don't get treated as a range in the smart match.

Well, to be honest I couldn't really get the ranges as keys thing
working so I don't know if it would really treat the key as a range.

Maybe clever is what I'm looking for. :-)

>> For more fun, say I want to store that table in a text file what is a
>> good way to represent that?
> 
>    I sort of liked your human readable format above.  JSON is fine but not as clear to the lay person.

I was using YAML for awhile but about 6 months or so ago I switched to
JSON. It's easier for me to understand than YAML (which has some white
space being important stuff that annoys me) is easily portable between
langues (especially javascript since it is really just javascript).



>    All-in-all could be a good challenge problem.

Mark thought so when I ask him.

Your web problem sounds interesting but I think I would need more of a
spec to understand the problem.

joe
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)
Comment: Using GnuPG with Red Hat - http://enigmail.mozdev.org

iD8DBQFL9qVJb0mzA2gRTpkRAtcQAJ9AkVP5cmY5uNj+THUhsGEAIHBt6wCbBKAF
wCqcBhLd5MnjFYVDVRMln4g=
=fBnA
-----END PGP SIGNATURE-----


More information about the Purdue-pm mailing list