r1n0 has asked for the wisdom of the Perl Monks concerning the following question:
#!/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";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Can I tie a hash of hashes to a file?
by moritz (Cardinal) on Nov 11, 2009 at 13:49 UTC | |
|
Re: Can I tie a hash of hashes to a file?
by davido (Cardinal) on Nov 11, 2009 at 13:55 UTC | |
|
Re: Can I tie a hash of hashes to a file?
by biohisham (Priest) on Nov 11, 2009 at 15:58 UTC | |
by r1n0 (Beadle) on Nov 12, 2009 at 18:58 UTC |