in reply to Need help with complex tied data structure

Have you paid attention to this part of the manual page:
BUGS 1. Adding or altering substructures to a hash value is not entirely transparent in current perl. If you want to store a reference or modify an existing reference value in the DBM, it must first be retrieved and stored in a temporary variable for further modifica­ tions. In particular, something like this will NOT work properly: $mldb{key}{subkey}[3] = 'stuff'; # won't wor +k Instead, that must be written as: $tmp = $mldb{key}; # retrieve +value $tmp->{subkey}[3] = 'stuff'; $mldb{key} = $tmp; # store val +ue This limitation exists because the perl TIEHASH inter­ face currently has no support for multidimensional ties.

Abigail

Replies are listed 'Best First'.
Re: Re: Need help with complex tied data structure
by genecutl (Beadle) on Nov 20, 2003 at 23:28 UTC
    Yes, I saw that. That's why I show the code at the end of my question with the $tmp variable. I tried pointing $tmp to different depths in the data structure, as well as having multiple temporary variables like:
    $tmp = $data[$gid]; $tmp2 = $tmp->{$id}; $tmp2->[$element_num] = $element;
    But none of that seemed to work.
      The fragment I posted also stresses out the importance of storing back the modified value in the top level hash. Your code doesn't do that.

      Abigail

        I see your point. So, then I should do this:
        $tmp = $data[$gid]->{$id}; $tmp->[$element_num] = $element; $data[$gid]->{$id} = $tmp;
        I haven't tested this yet, but while typing it, I noticed that with the $tmp assignment, that I'm pulling all of @{$data[$gid]->{$id}} into memory, partly obviating the advantage of the disk-based tie. It would make more sense then to generate the entire @{$data[$gid]->{$id}} array in memory and only save it when all the elements are computed. Is that correct?