> "Edward M. Perry" <eperry@learjet.com> writes: > > > Here is the problem: given a key ($key), and a hash (%h), I want the value > > of the closest hash key. I came up with this which is only moderately fun. > > Really it works pretty well, assuming keys are numeric and the hash is > > small. Mapping to %temp is weak. > > > > An array seems unreasonable because in reality the keys (or indicies) are > > large and the array would be very sparse. > >[hashing example snipped] > > It sounds like you want (gasp!) a data structure that isn't an array > or a hash. A binary tree would be a good choice. If linear-time is > acceptable, or if the keys arrive in reasonably "random" order, then > it's definitely good enough; otherwise you may want to rotate it. > > How many items have you got? > > -- > Ariel Scolnicov |"GCAAGAATTGAACTGTAG" |ariels@compugen.co.il > Compugen Ltd. |Tel: +972-2-6795059 (Jerusalem) \ NEW IMPROVED URL! > 72 Pinhas Rosen St. |Tel: +972-3-7658520 (Main office)`-------------------- > Tel-Aviv 69512, ISRAEL |Fax: +972-3-7658555 http://3w.compugen.co.il/~ariels The real problem is pretty small, about 100 keys, 20 lookups, and speed is not really a concern. A B-Tree would be overkill for this problem, but probably would be a good solution for a larger data set. Your suggestion did get me thinking about good ways to binary search a set of hash keys though...Gotta go do it. Thanks. ==== Want to unsubscribe from Fun With Perl? Well, if you insist... ==== Send email to <fwp-request@technofile.org> with message _body_ ==== unsubscribe