in reply to file merge problem

Hi, If i understood your question correctly, this will help you.

open F1,">>F1.dat" or die "Can't open F1.dat: $!\n"; #Open F1 file in +Append mode open F2,"F2.dat" or die "Can't open F2.dat: $!\n"; while(<F2>) { print F1 $_; } close(F1); close(F2);

(Untested)

Regards,
Velusamy R.


eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

Replies are listed 'Best First'.
Re^2: file merge problem
by Anonymous Monk on Dec 09, 2005 at 13:14 UTC
    My code creates the new file so I don't get duplicates and also deals with orphaned records i.e. those that only live in one file but not the other
    Hence this doesn't help. Thanks anyway

      You might want to look again at Samy_rio's solution above -- with a little tweaking, you could address the duplicate issue. I don't think that you will have a problem with orphaned records using the proposed file concatenation.

      If you open the first file, step through it and create a lookup hash, then close and re-open the first file for appending and the second file for reading, you can simply check each line of the second file to see if it is already in your lookup hash before appending it to the first file.

      You don't tell us what impact the additional words have in terms of the logic of your program -- if they are simply extra fields that you want to bring forward, then you can simply adjust your existing regular expression from (\d*) to (\d+.*) to include everything to the end of the line, something like this:

      if (/^(\w*) (\d+.*)$/) { unless (exists($lookup{$1}) && ($lookup{$1} eq $2)) { print F1, $_; } } } else { warn "Invalid input line $.: $_\n"; }

      If the additional words in the line have some impact on your program's logic (e.g., if you need to parse them off and possibly create new records in your resulting file) then you need to tell us.

      Hope that helps. :)

      Update: added 'exists' logic to check in lookup hash.


      No good deed goes unpunished. -- (attributed to) Oscar Wilde
        Forgive me I'm fairly new to both Perl and this (excellent) website of yours
        In answer to your question

        The additional words have no impact to the logic of my program. They are as you have suggested merely extra fields that I need to bring forward.
        Making the simple change you suggested has worked. Thanks