Your SlideShare is downloading. ×
dbm.doc
dbm.doc
dbm.doc
dbm.doc
dbm.doc
dbm.doc
dbm.doc
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

dbm.doc

204

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
204
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. #!/usr/local/bin/perl -w use Getopt::Long; use DB_File; use Fcntl; # Set up the command line options. my $ret = GetOptions ("f|filename:s", "d|database:s"); my $filename = $opt_f || die "Usage: $0 -f filename -d databasen"; my $database = $opt_d || die "Usage: $0 -f filename -d databasen"; my %inventory; # Open the input file. open (INPUT, "$filename") || die "Error: Can not open the file $filename : $!n"; # Open the DBM database file. tie %inventory, DB_File, $database, O_RDWR|O_CREAT, 0700, $DB_BTREE || die "Error: Can not open the DBM database $database : $!n"; # Loop through the input file and put it into the DBM database. while (<INPUT>) { chomp; # Split the line my ($number,$type,$name,$price,$desc) = split (/|/); # Save the information into the DBM file. $inventory{$number} = "${type}|${name}|${price}|${desc}"; } # Close the dbm database untie %inventory; # Close the input file. close (INPUT); #!/usr/sbin/perl -w # Use the DBM module. use Getopt::Long; use DB_File; # Set up the command line options. my $ret = GetOptions ("f|filename:s", "d|database:s"); my $filename = $opt_f || die "Usage: $0 -f filename -d databasen"; my $database = $opt_d || die "Usage: $0 -f filename -d databasen"; my %inventory; # Open the input file. open (INPUT, "$filename") || die "Can not open the file $filename : $!n"; # Open the DBM database file.
  • 2. dbmopen (%inventory, $database, 0700) || die "Can not open the DBM database $database : $!n"; # Loop through the input file and put it into the DBM database. while (<INPUT>) { chomp; # Split the line my ($number,$type,$name,$price,$desc) = split (/|/); # Save the information into the DBM file. $inventory{$number} = "${type}|${name}|${price}|${desc}"; } # Close the dbm database dbmclose %inventory; # Close the input file. close (INPUT); #!/usr/local/bin/perl -w # Use the DBM module. use Getopt::Long; use English; use DB_File; use Fcntl; # Set up the command line options. my $ret = GetOptions ("d|database:s", "i|index:s"); my $database = $opt_d || die "Usage: $0 -d database -i indexn"; my $index = $opt_i || die "Usage: $0 -d database -i indexn"; # Open the DBM database file. tie (%inventory, DB_File, $database, O_RDWR|O_CREAT, 0700) || die "Could not open DBM file $database : $!n"; # Does the record exist? die "$0: Record key $index does not exist in the database.n" if (! defined $inventory{$index}); # Remove the original record from the database. delete $inventory{$index}; # Close the dbm database untie %inventory; #!/usr/local/bin/perl -w # Use the DBM module. use Getopt::Long; use English; use DB_File; use Fcntl;
  • 3. # Set up the command line options. my $ret = GetOptions ("d|database:s"); my $database = $opt_d || die "Usage: $0 -d databasen"; my (%contents,$count,$type,$name,$price,$desc,$record); # Open the DBM database file. tie %contents, DB_File, $database, O_RDWR|O_CREAT, 0700, $DB_BTREE || die "Could not open DBM file $database : $!n"; # Force the top of page. $FORMAT_LINES_LEFT = 0; $count = 0; # Start printing out the dbm information. for $number (keys %contents) { # Get the contents from the associative array. $record = $contents{$number}; # Split the record up. ($type,$name,$price,$desc) = split (/|/, $record); $count++; # Write it... write; } # Close the DBM database. untie %contents; # Top of picture formats. format STDOUT_TOP= Count Item # Item Type Item Name Price Description Page @>>>>>> $FORMAT_PAGE_NUMBER ========================================================================= === . format STDOUT= @>>>> @|||||| @<<<<<<<<< @<<<<<<<<<<<<< $@####.## ^<<<<<<<<<<<<<<<<<<<<<<<<< $count,$number,$type,$name,$price,$desc ~ ^<<<<<<<<<<<<<<<<<<<<<<<<< $desc ~ ^<<<<<<<<<<<<<<<<<<<<<<<<< $desc . #!/usr/local/bin/perl -w # Use the DBM module. use Getopt::Long;
  • 4. use DB_File; use Fcntl; # Set up the command line options. my $ret = GetOptions ("d|database:s"); my $database = $opt_d || die "Usage: $0 -d databasen"; my %inventory; # Open the DBM database file. tie (%inventory, DB_File, $database, O_RDWR, 0700) || die "Could not open DBM file $database : $!n"; # Print out the DBM record count. my $recordCount = keys %inventory; print "Before: DBM database record count = $recordCountn"; # Remove all the records from the database. undef %inventory; # Print out the DBM record count. $recordCount = keys %inventory; print "After : DBM database record count = $recordCountn"; # Close the dbm database untie %inventory; #!/usr/sbin/perl -w # Use the DBM module. use Getopt::Long; use English; use DB_File; # Set up the command line options. my $ret = GetOptions ("d|database:s"); my $database = $opt_d || die "Usage: $0 -d databasen"; my (%contents,$count,$type,$name,$price,$desc,$record); # Open the DBM database file. dbmopen (%contents, $database, 0700) || die "Could not open DBM file $database : $!n"; # Force the top of page. $FORMAT_LINES_LEFT = 0; $count = 0; # Start printing out the dbm information. for $number (sort keys %contents) { # Get the contents from the hash. $record = $contents{$number}; # Split the record up. ($type,$name,$price,$desc) = split (/|/, $record); $count++;
  • 5. # Write it... write; } # Close the DBM database. dbmclose %contents; # Top of picture formats. format STDOUT_TOP= Count Item # Item Type Item Name Price Description Page @>>>>>> $FORMAT_PAGE_NUMBER ========================================================================= === . format STDOUT= @>>>> @|||||| @<<<<<<<<< @<<<<<<<<<<<<< $@####.## ^<<<<<<<<<<<<<<<<<<<<<<<<< $count,$number,$type,$name,$price,$desc ~ ^<<<<<<<<<<<<<<<<<<<<<<<<< $desc ~ ^<<<<<<<<<<<<<<<<<<<<<<<<< $desc . #!/usr/local/bin/perl -w # Use the DBM module. use Getopt::Long; use English; use DB_File; use Fcntl; # Set up the command line options. my $ret = GetOptions ("db1:s", "db2:s", "result:s"); my $db1 = $opt_db1 || die "Usage: $0 --db1 database --db2 database --result databasen"; my $db2 = $opt_db2 || die "Usage: $0 --db1 database --db2 database --result databasen"; my $result = $opt_result || die "Usage: $0 --db1 database --db2 database --result databasen"; my (%inv1, %inv2, %resultant, $key); # Open the first DBM file. tie (%inv1, DB_File, $db1, O_RDONLY, 0700) || die "Could not open DBM file $db1 : $!n"; # Open the second DBM file. tie (%inv2, DB_File, $db2, O_RDONLY, 0700) || die "Could not open DBM file $db2 : $!n";
  • 6. # Open the resultant DBM file. tie (%resultant, DB_File, $result, O_RDWR|O_CREAT, 0700) || die "Could not open DBM file $result : $!n"; # Merge in the first DBM file. for $key (keys %inv1) { if (defined $resultant{$key}) { print "Error: The key $key already exists in the final DBM file. Ignoring record.n"; } else { $resultant{$key} = $inv1{$key}; } } # Merge in the second DBM file. for $key (keys %inv2) { if (defined $resultant{$key}) { print "Error: The key $key already exists in the final DBM file. Ignoring record.n"; } else { $resultant{$key} = $inv2{$key}; } } # Close all the databases. untie %inv1; untie %inv2; untie %resultant; #!/usr/local/bin/perl -w # Use the DBM module. use Getopt::Long; use DB_File; use Fcntl; # Set up the command line options. my $ret = GetOptions ("d|database:s", "i|index:s"); my $database = $opt_d || die "Usage: $0 -d database -i indexn"; my $index = $opt_i || die "Usage: $0 -d database -i indexn"; my %inventory; # Open the DBM database file. tie %inventory, DB_File, $database, O_RDWR|O_CREAT, 0700 || die "Could not open DBM file $database : $!n"; # Does the record exist?
  • 7. die "$0: Record key $index does not exist in the database.n" if (! defined $inventory{$index}); # Change the record. $inventory{$index} = uc $inventory{$index}; # Close the dbm database untie %inventory; #!/usr/local/bin/perl -w # Purpose: # This script demonstrates how to perform a # 'sounds-like' pattern match on a DBM file. # Use the DBM module. use Getopt::Long; use DB_File; use Fcntl; # Set up the command line options. my $ret = GetOptions ("d|database:s", "p|pattern:s"); my $database = $opt_d || die "Usage: $0 -d database -p patternn"; my $pattern = $opt_p || die "Usage: $0 -d database -p patternn"; # Open the DBM database file. $db = tie (%inventory, DB_File, $database, O_RDWR|O_CREAT, 0700) || die "Could not open DBM file $databasen"; # Start looking for the pattern we were given. for $key (keys %inventory) { # Get the record from the DBM file. $db->get ($key, $record); # Look for the 'sounds-like' pattern... if ($record =~ $pattern) { print "Match found ($pattern) =~ <$record>n"; } } # Close the dbm database untie (%inventory);

×