Tie hash of hash using MLDBM 
Author Message
 Tie hash of hash using MLDBM

I'm facing problem in tie a has of hash to disk file.
snip
use MLDBM qw(DB_File Storable);
tie %HoH, 'MLDBM', 'mldbm_file', O_CREAT|O_RDWR, 0640;
....
Read from text file: $name $value1 $value2

$HoH{name}{value1} = value2;

___
If I read the 'mldbm_file' only keys come not values,

BUT if I assign like
%HoH = (
        name1 =>     { val1 => val2, val3 => val4, },
        name2L => { val1 => val2, val3 => val4, }

    );
I get the value hashes as well.

Thanks

Debjit

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Thu, 29 May 2003 21:41:55 GMT  
 Tie hash of hash using MLDBM

Quote:

>I'm facing problem in tie a has of hash to disk file.
>snip
>use MLDBM qw(DB_File Storable);
>tie %HoH, 'MLDBM', 'mldbm_file', O_CREAT|O_RDWR, 0640;
>....
>Read from text file: $name $value1 $value2

>$HoH{name}{value1} = value2;

Did you really mean to use bare words there?
I suppose that you really meant:

    $HoH{$name}{$value1} = $value2;

Quote:
>If I read the 'mldbm_file' only keys come not values,

>BUT if I assign like
>%HoH = (
>        name1 =>     { val1 => val2, val3 => val4, },
>        name2L => { val1 => val2, val3 => val4, }

>    );
>I get the value hashes as well.

I'd recommend coding up the smallest reasonable demo that illustrates
the problem. As you are writing it you may find the problem your
self.  If you dont you'll have a nice small piece of code that you
can post that will make it clearer to us what is going on.

chris
--
    This space intentionally left blank



Fri, 30 May 2003 04:43:11 GMT  
 Tie hash of hash using MLDBM
Quote:

> I'm facing problem in tie a has of hash to disk file.
> snip
> use MLDBM qw(DB_File Storable);
> tie %HoH, 'MLDBM', 'mldbm_file', O_CREAT|O_RDWR, 0640;
> ....
> Read from text file: $name $value1 $value2

> $HoH{name}{value1} = value2;

> ___
> If I read the 'mldbm_file' only keys come not values,

> BUT if I assign like
> %HoH = (
>         name1 =>     { val1 => val2, val3 => val4, },
>         name2L => { val1 => val2, val3 => val4, }

>     );
> I get the value hashes as well.
...
> Debjit

...
That's right.  Read the docs:

    perldoc MLDBM

looking in particular at the "BUGS" section (even though it isn't really
a bug).  Basically, due to the nature of how MLDBM works, it is
necessary to retrieve and store only complete structures under a given
top-level key.  For example, to change $HoH{name}{value1}, do
[untested]:

    my $tempref=$HoH{name};
    $tempref->{value1}={value2};
    $HoH{name}=$tempref;

What MLDBM is really doing is simply passing whatever it gets or
retrieves to/from keys through Data::Dumper (or Storable or FreezeThaw)
for serialization. That only works when getting or retrieving keys --
tie doesn't see attempts to access substructures of the hash, and hence
they don't get processed and retrieved from/written to the file.  HTH.
--
Bob Walton



Fri, 30 May 2003 05:08:27 GMT  
 Tie hash of hash using MLDBM


Wed, 18 Jun 1902 08:00:00 GMT  
 Tie hash of hash using MLDBM
Many thanks Bob,... you solved the editing problems i was trying with
MLDBM for me BIG-TIME!

here is my WORKING code:---
###remember the "$tempref" variable is equal TO YOUR top level key in
##your MLDBM::never refer to the temp with it's KEYNAME, only
##reference the lower levels as in the 4th line!
##when writing BACK to original MLDBM key
##ONLY THEN can you refer to that key again! line 5
###this script checks to see if the 'email/key' exists in the MLDBM
###thenifso adds a new key into the right here designated place in the
###lower level of the MLDBM. otherwise creates a whole new key/entry

tie %Customers, 'MLDBM', $friends, O_CREAT | O_RDWR, 0666;
        if (exists($Customers{$input{'email'}})) {
                $tempref = $Customers{$input{'email'}};
                $tempref->{'ads'}{$adnum} = $theirdata;
                $Customers{$input{'email'}} = $tempref;
        }else{
                $Customers{$input{'email'}} = {
                        nom => $input{'name'},
                        phn => $input{'phone'},
                        add => $input{'address'},
                        cty => $input{'city'},
                        try => $input{'country'},
                        zip => $input{'code'},
                        buk => '0',
                        ads => {
                                $adnum => $theirdata
                        }
                };
        }
untie %Customers;

ps:  anyone know if DELETING can be spliced in ok?

On Sun, 10 Dec 2000 21:08:27 GMT, Bob Walton

Quote:

>looking in particular at the "BUGS" section (even though it isn't really
>a bug).  Basically, due to the nature of how MLDBM works, it is
>necessary to retrieve and store only complete structures under a given
>top-level key.  For example, to change $HoH{name}{value1}, do
>[untested]:

>    my $tempref=$HoH{name};
>    $tempref->{value1}={value2};
>    $HoH{name}=$tempref;

>What MLDBM is really doing is simply passing whatever it gets or
>retrieves to/from keys through Data::Dumper (or Storable or FreezeThaw)
>for serialization. That only works when getting or retrieving keys --
>tie doesn't see attempts to access substructures of the hash, and hence
>they don't get processed and retrieved from/written to the file.  HTH.
>--
>Bob Walton



Thu, 05 Jun 2003 17:34:45 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. bizarre(?) problem, tied hash of tied hashes

2. Tying Multidimensional hashes with MLDBM

3. Getting fd from tie(%hash,MLDBM,$file,O_RDWR,0666)

4. Hash of Hashes possible w/MLDBM?

5. using IPC::Shareable and using Tie::IxHash for the same hash

6. tie a hash in a hash

7. tied hashed hash looses item #1

8. perl5.004_04 bug/weakness?: tie %hash and scalar(%hash)

9. Tie a hash of hashes ?

10. Tie a hash of hash of array

11. tied hash of hashes

12. Hashes of Hashes of Hashes of Hashes....

 

 
Powered by phpBB® Forum Software