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


In reply to Re: Compare Two Files, Merge Updates from One File to the Other by ELISHEVA
in thread Compare Two Files, Merge Updates from One File to the Other by rycher

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.