Hello again Monks,

I want to start using hashes a bit for some coding I am doing to keep a list of friends with information I can use to better keep in touch with them. I want to create this with thoughts of expanding this later. I have used DB_File to tie a hash to a file. Thanks for the help with that fellow monks. Now, I want to know if I can tie a hash of hashes to a file. I tried to use DB_File but with no luck. When the file is read, it identifies the entries are there, but fails to return any of the information for each entry. Here is the code I am playing with.

#!/usr/bin/perl -w # The information contained within this file is fictional # No intent to link this information to real people has been made #use strict; use warnings; # The following module was created to help tie hashes to files use DB_File; my $showOutput = 1; my $start_time = time(); # Name of the file that will be tied to the hash my $hash_filename = "hash_of_hashes.dbf"; my $remove_hash = shift; if ( $remove_hash eq "NEW" ){ print STDOUT "Deleting old hash file\n" if $showOutput; unlink $hash_filename or die "Can not remove $hash_filename\n\n"; } else{ print STDOUT "Using old hash file\n" if $showOutput; } # Tie the file to the hash tie my %hash, 'DB_File', $hash_filename, O_RDWR|O_CREAT, 0666, $DB_HAS +H or die "Problem tying \%hash: $!\n\n"; if ( exists $hash{"Janet Jones"} ){ print STDOUT "JJ has already been defined\n"; # Do nothing, person is already defined } else{ print STDOUT "JJ does NOT exist... defining\n"; $hash{"Janet Jones"} = { "spouse" => "Bob Jones", "son 1" => "Tom Jones", "son 2" => "Pete Jones", "pet 1" => "Boyle", "father" => "Richard Smith", "mother" => "Fran Smith", "sister 1" => "Louise Williams", "address" => "1 Main St, Littleton, PA, 55555", }; } if ( exists $hash{"Bob Jones"} ){ print STDOUT "BJ has already been defined\n"; # Do nothing, person is already defined } else{ print STDOUT "BJ does NOT exist... defining\n"; $hash{"Bob Jones"} = { "spouse" => "Janet Jones", "son 1" => "Tom Jones", "son 2" => "Pete Jones", "pet 1" => "Boyle", "father" => "Paul Jones", "mother" => "Stella Jones", "brother 1" => "Paul Jones, Jr", "brother 2" => "Vince Jones", "brother 3" => "Dan Jones", "sister 1" => "Andrea Limux", "address" => "1 Main St, Littleton, PA, 55555", }; } for my $name ( keys %hash ){ print STDOUT "$name:\n"; for my $info ( sort keys %{ $hash{$name} } ) { print "\t$info: " . $hash{$name}{$info} . "\n"; } } untie %hash; my $end_time = time(); print STDOUT "\n\n"; print STDOUT "Start Time: $start_time\n"; print STDOUT "End Time: $end_time\n"; print STDOUT "Total Time: " . ($end_time - $start_time) . " seconds\n\ +n";


I don't have to use DB_File. Since I used it for tying a hash to a file, I was thinking it would support tying hash of hashes to a file, too. Any insights in this area would be very much appreciated. If I don't tie the file to the hash, and just keep the hash in memory, this all works fine, but I want to tie it to a file to keep this as a record for updates, etc.

As always, thank you very much for your help.

In reply to Can I tie a hash of hashes to a file? by r1n0

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.