If the files are sorted, your quickest option will probably be to take the first line from each file, print the line from file1 if they're different or advance a line in file2 if they're the same, and then advance a line in file1. This is also very space-efficient, since you only need to have one line from each file in memory at a time.
A basic implementation of this would be:
#!/usr/bin/perl -w
use strict;
open FILE1, '<', 'file1' || die "Can't open file1: $!";
open FILE2, '<', 'file2' || die "Can't open file2: $!";
my $line1 = <FILE1>;
my $line2 = <FILE2>;
while ($line1 && $line2) {
if ($line1 == $line2) {
$line1 = <FILE1>;
$line2 = <FILE2>;
} else {
print $line1;
$line1 = <FILE1>;
}
}
print $line1 if defined $line1;
while ($line1 = <FILE1>) {
print $line1;
}
Note that this implementation assumes that there are no values in file2 which are not also present in file1 and that neither file contains any duplicates.
If the files are not sorted (and you're not going to be using them repeatedly), then a hash-based solution such as others have proposed would probably be faster than sorting them and using this method.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.