Recipe 14.4 Merging DBM Files

14.4.1 Problem

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

14.4.2 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;         }     } }

14.4.3 Discussion

This straightforward application of Recipe 5.11 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.

14.4.4 See Also

Recipe 5.11; Recipe 14.6



Perl Cookbook
Perl Cookbook, Second Edition
ISBN: 0596003137
EAN: 2147483647
Year: 2003
Pages: 501

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net