js1 has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Monks,

I have a problem with a hash of arrays. Here's my code:

tie %recipientqids, 'MLDBM', "/home/u752359/recipientqids.db", O_CREAT +|O_RDWR, 0640 or die $!; .. .. push(@{$recipientqids{$hostqid}},$recipient); .. .. foreach $recipient (@{$recipientqids{$hostqid}}){ print STDERR "\nrecip= $recipient"; }

If I tie the hash it doesn't print anything, but if I don't tie it, it does print out. Do I need to change my syntax if I tie the hash?

Thanks for any help.

JS.

Replies are listed 'Best First'.
Re: hash of arrays syntax
by Zaxo (Archbishop) on Jan 15, 2004 at 10:57 UTC

    See the BUGS section of the MLDBM docs. You need to extract the whole array into a temporary variable and push onto that, then assign an array reference to the result back to the tied hash.

    From the MLDBM pod:

    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 modifications. In particular, something like this will NOT work properly:         $mldb{key}{subkey}[3] = 'stuff';        # won't work Instead, that must be written as:
    $tmp = $mldb{key}; # retrieve value $tmp->{subkey}[3] = 'stuff'; $mldb{key} = $tmp; # store value
    This limitation exists because the perl TIEHASH interface currently has no support for multidimensional ties.

    After Compline,
    Zaxo

      Thanks, that's fixed it for me now.

      JS.