Herong's Tutorial Notes on Perl - Part A
Dr. Herong Yang, Version 4.09

Working with DBM Files

Part:   1  2  

(Continued from previous part...)

Run this program, and print dictionary again. You will see that the new entry is inserted at the end of the file:

>DbmUpdate.pl dictionary

>dbmprint.pl dictionary
bible = bible
computer = ordinateur
good = bon
milk = lait
price = prix
ten = dix
cloth = vetement

The fourth program, DbmLookup.pl, looks up a word from the dictionary:

#- DbmLookup.pl
#- Copyright (c) 1996 by Dr. Herong Yang
#
   ($name, $key) = @ARGV;
   die "Missing DBM name.\n" unless $name;
   die "Missing key.\n" unless $key;
   dbmopen(%map,$name,0666);
   if ($map{$key}) {
      print($key, ' = ', $map{$key}, "\n");
   } else {
      print($key, " = (not defined)\n");
   }
   dbmclose(%map);
   exit;

Try it with a couple of words:

>dbmlookup.pl dictionary apple
apple = (not defined)

>dbmlookup.pl dictionary milk
milk = lait

DBM Example - Book Table

This DBM example will show you how to manage a table with multiple columns. In the following Perl program, BookInsert.pl, I joined all columns into a single string with "\n" as the delimiter, and stored it into the hash:

#- DbmInsert.pl
#- Copyright (c) 1996 by Dr. Herong Yang
#
   ($name) = @ARGV;
   die "Missing DBM name.\n" unless $name;
   dbmopen(%map,$name,0666);
   $id = &lastKey();
   $id++;
   $key = $id;
   $title = "Programming Perl";
   $author = "Larry Wall";
   $date = "Jan 1991";
   $isbn = "0937175641";
   $edition = "1";
   $val = join("\t",$title,$author,$date,$edition,$isbn);
   $map{$key} = $val;
   $id++;
   $key = $id;
   $title = "Learning Perl";
   $author = " Randal L. Schwartz";
   $date = "Nov 1993";
   $isbn = "1565920422";
   $edition = "3";
   $val = join("\t",$title,$author,$date,$edition,$isbn);
   $map{$key} = $val;
   dbmclose(%map);
   exit;
sub lastKey {
   local $max = 0;
   local $key, $val;
   while (($key,$val)=each(%map)) {
      $max = $key if ($key>$max);
   }
   return $max;   
}

To bring back individual columns from the hash, I used the split function as shown in the following program, BookPrint.pl

#- BookPrint.pl
#- Copyright (c) 1996 by Dr. Herong Yang
#
   ($name) = @ARGV;
   die "Missing DBM name.\n" unless $name;
   dbmopen(%map,$name,0666);
   $rec = 0;
   while (($key,$val)=each(%map)) {
      $rec++;
      $id = $key;
      ($title,$author,$date,$edition,$isbn) = split("\t",$val);
      print "\nRecord: $rec\n";
      print "        id = $id\n";
      print "    author = $author\n";
      print "      date = $date\n";
      print "   edition = $edition\n";
      print "      isbn = $isbn\n";
   }
   dbmclose(%map);
   exit;

Here is the output of running these programs together:

>bookinsert.pl bookbase

>bookprint.pl bookbase

Record: 1
        id = 1
    author = Larry Wall
      date = Jan 1991
   edition = 1
      isbn = 0937175641

Record: 2
        id = 2
    author =  Randal L. Schwartz
      date = Nov 1993
   edition = 1
      isbn = 1565920422

Part:   1  2  

Dr. Herong Yang, updated in 2006
Herong's Tutorial Notes on Perl - Part A - Working with DBM Files