in reply to Best Multidimensional Hash Practices?

One useful practice for multidimensional hashes is not to use them indiscriminately. There's a tendency to make enormous, monolithic hashes that some people (myself included) sometimes exhibit, something like:

$user_data{$username}={"date of birth"=>$dob, "ID number"=>$idnum, "mailing address"=>[$street, $citystatezip, ], };

when it might (and probably would) be better to do something like:

$user_dob{$username}=$dob; $user_idnum{$username}=$idnum; $user_mailaddress{$username}=[...];

The obvious symptom of this, when I make this mistake, is a large number of constant strings for subkeys in a hash where the top level keys are of interest. It's just as easy to tie multiple hashes with nice meaningful names together by their keys as it is to keep it all in one hash where the meaning is split between the hash name and second/third level keys, and it's much easier to handle.

Replies are listed 'Best First'.
Re^2: Best Multidimensional Hash Practices?
by GrandFather (Saint) on Oct 12, 2009 at 21:36 UTC

    The better solution is to use an object for the user and hide the nasty representation issues from client code that uses the object.

    Using multiple data structures in parallel is always fraught because it is so easy to get them out of sync. Despite the complexity of the one data structure approach, it is generally better to use it and avoid the nastiness of either a slew of globals or ensuring that you always pass the whole suite of variables around correctly.


    True laziness is hard work