Good afternoon,
On 11/12/03 at 4:55 PM -0800, wren argetlahm <[EMAIL PROTECTED]> wrote:
>--- Charlie Garrison <[EMAIL PROTECTED]> wrote:
>> I'm no expert (& I'd love to hear from one). But
>> when I need to do something
>> similar I use a second data structure for doing the
>> 'reverse lookup'.
>
>I thought about that, but was put off by the fact that
>it's a complex structure-- of course I'm only
>reverse-looking up one field, so it might work. I have
>an estimated 1-10 lookups per database entry, of which
>there are ~500 right now. So there might be time
>savings, but memory requirements might be an issue
><contemplating/>
Exactly, which is why I ask that you post the solution here if you find
something more elegant. It feels like a sledgehammer solution to me, but I've
yet to find anything better.
>--- Fred Eiden <[EMAIL PROTECTED]> wrote:
>> How about using a different data structure? Maybe a
>> hash of hashes would
>> work better. If the value of $xml_index is unique,
>
>$xml_index is unique (as is the name value), but the
>reason for the array is to sort the entries into an
>order which doesn't necessarily correlate with
>$xml_index or the name, and I look things up by their
>order more often than their $xml_index.
I also agree with Fred's solution, but only for data which doesn't need to be
sorted (which I assumed was the case for you). It is possible to expand on
Fred's solution to save the initial ordering by also using a 'sort_val' key
into each hash. But that starts getting just as complex:
%names = ({sort_val=>1, data=>{xml_index=>'a', name=>'abc'}},
{sort_val=>2, data=>{xml_index=>'d', name=>'def'}} );
But then you need to do the same thing for each key that you need to do
lookups for. Each 'data' hash can be referred to with references so that you
only need one copy across all lookup hashes, but it still uses more memory.
Can anyone offer an elegant solution for a data structure that maintains
sorted order as well as access to data for a (primary) key?
Charlie
--
Charlie Garrison [EMAIL PROTECTED]
PO Box 141, Windsor, NSW 2756, Australia