Приглашаем посетить
Биология (bio.niv.ru)

Recipe 14.4. Merging DBM Files

PreviousChapter 14
Database Access
Next
 

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


PreviousHomeNext
14.3. Converting Between DBM FilesBook Index14.5. Locking DBM Files