Recipe 14.4. Merging DBM Files
Author: Tom Christiansen & Nathan Torkington
Date: 1999-07-02T01:42:48Z

#

| Chapter 14 Database Access |

## 14.4. Merging DBM Files

### Problem

You want to combine two DBM files into a single DBM file with original key/value pairs.

### Solution

Either merge the databases by treating their hashes as lists:

    %OUTPUT = (%INPUT1, %INPUT2);

or, more wisely, by iterating over each key-value pair.

    %OUTPUT = ();
    foreach $href ( \%INPUT1, \%INPUT2 ) {
        while (my($key, $value) = each(%$href)) {
            if (exists $OUTPUT{$key}) {
                # decide which value to use and set $OUTPUT{$key} if necessary
            } else {
                $OUTPUT{$key} = $value;
            }
        }
    }

### Discussion

This straightforward application of Recipe 5.10 comes with the same caveats. Merging hashes by treating them as lists requires that the hashes be preloaded into memory, creating a potentially humongous temporary list. If you're dealing with large hashes, have little virtual memory, or both, then you want to iterate over the keys with `each` to save memory.

Another difference between these merging techniques is what to do if the same key exists in both input databases. The blind assignment merely overwrites the first value with the second value. The iterative merging technique lets you decide what to do. Possibilities include issuing a warning or error, choosing the first over the second, choosing the second over the first, or concatenating the new value to the old one. If you're using the MLDBM module, you can even store them both, using an array reference to the two values.

### See Also

Recipe 5.10; Recipe 14.8

14.3. Converting Between DBM Files |  | 14.5. Locking DBM Files