in reply to Re^2: Dotted hash access
in thread Dotted hash access

The more I read this thread the more I dont understand why you dont maintain a hash that is structured with only two levels, location and then building. Then your code looks like:

my $code=$locations{$location}{$building}{code};

Also something to keep in mind (although its not hugely critical) each deref takes time, each hash lookup takes time, each unique key takes space. So in some circumstances your dotted approach would result in considerably more memory being taken up by the keys. Not only that but determinisitc traversal of your dotted form of the tree would be quite expensive as compared to the non dotted form. Overall I wouldnt go this route unless i had really strong justification to do so. And style isnt a strong justification IMO :-)

---
demerphq

Replies are listed 'Best First'.
Re^4: Dotted hash access
by sfink (Deacon) on Dec 03, 2004 at 04:24 UTC
    The only way in which my exact data structure is relevant is if nothing like it should ever be created -- i.e., if I am making it far more complicated than it needs to be. I haven't given enough details about my data for anyone to come to that conclusion. That wasn't wholly unintentional. I wanted the meditation to be about a general problem that I occasionally encounter, and was wondering if other people also encountered.

    For the record, your proposed refactoring of the data structure wouldn't work in my case because 'Locations' is one key of many at the top level. The whole structure is intended to represent an empire, which controls a set of locations, has various technologies, a name, etc. Some of those are simple values, some are themselves nested structures. A 'Location' (identified by a name) has a set of buildings, but also resources, a description, an inventory, etc. So simplifying things in the manner you suggest would result in Locations colliding with the strings 'Name', 'Technologies', etc.

    As for dereferences taking time -- um, my module uses a hash tie, and on every lookup it splits apart the key and does a recursive lookup. So it's far, far slower than just a set of dereferences! Performance is really not a concern. This is an exploration into readability.

    But your comment about keys suggests that you misunderstand what I'm doing. The actual data structure is merely a HoHoHo...H. I am never storing any keys in their dotted form. Doing so would prevent me from grabbing out pieces of the structure:

    $location_info = $h->{"Locations.$location"}; print "$location description: $location_info->{Description}\n";
    I am just providing a hash reference that, in addition to being addressable the normal way, can also be addressed with an alternate dotted syntax. The internal structure is unchanged, and a deterministic traversal just requires a regular ASCIIbetical sort of the keys at each level (same as any other HoH.)

    Sorry if this wasn't clear from the original post.