in reply to Compare Two Files, Merge Updates from One File to the Other

My question is: How would I open both files, read in all of the contents into arrays, key in on the username as a match-point and update the LDIF file with data from the employee database?
  1. open a file - see perlopentut and open
  2. read contents into arrays As graff has pointed out, arrays aren't the best data structure when you need to compare something in the line of one file with something in the lines of another file. The preferred proceedure is to
    1. read in a record at a time from the first file. Your main challenge here is figuring out when a record has ended. For example, if all records begin with dn: and that string is never found inside a record, you could set the record separator ($/) to "dn:", e.g. local $/='dn:';. If 'dn: uid' is the never-repeated string between records, then use that . Each time Perl reads a "line" it will read until it finds that separator. see readline and perlvar for more information. Note: if there is no fixed string, or if you need to use a regular expression to insure that you only break records when dn: is at the start of a line, please update your post, the techniques for such complicated files are a bit different.
    2. parse the record to extract the user name - see chomp, split, and perlretut for various tools you can use to do that.
    3. store the record in a hash containing username-record pairs, like this: $hKeyedLines{$username}=$record; - see perldata and perldsc for more information on hashes and how to use them.
  3. key in on the user name as a match point - read in the second file( or process database query results) record by record but this time instead of storing a record in an array: (a) parse each record in the second file to extract the username (b) look up the user name in the hash you created from the first file, like this: $lineFromFirstFile = $hKeyedLines{$username}. Once you have looked up the record from the first file,
    1. break up both records into fields
    2. choose which value you prefer from each record
    3. generate a new version of that record. If you store the selected values in variables, you might want to use an interpolated string, e.g. "dn: uid: $username ..." - Given the length of each record you may also want to consider a "here" document. See perlop for more on interpolated strings and "here" documents. Also, you might find perlfaq4: Why don't my <<HERE documents work? helpful.
  4. update the LDIF file - open a temporary file for writing, write out the merged lines to that file. Then when the file is complete, rename the temporary file to the real file. Using a temporary file protects the original file from corrupting in case your program crashes part way through. See perlopentut (again - but this time focus on the bits about opening to write files) and rename. Use File::Temp to generate a name for the temporary file.

Best, beth