Hi!
Look at this. Probably it helps to find the way.
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my $filename1 = 'file1.txt';
my $filename2 = 'file2.txt';
open my $fhin1, "<", $filename1 or die "ERROR: Can't open file '$filen
+ame1': $!";
open my $fhin2, "<", $filename2 or die "ERROR: Can't open file '$filen
+ame2': $!";
my %lines_found_in_file1 = map {
chomp;
s/^\s+//;
s/\s+$//;
$_ => 1
} (<$fhin1>);
my %lines_found_in_file2 = map {
chomp;
s/^\s+//;
s/\s+$//;
$_ => 1
} (<$fhin2>);
close $fhin1 or die "ERROR: Couldn't close file '$filename1': $!";
close $fhin2 or die "ERROR: Couldn't close file '$filename2': $!";
my $output1 = 'common.txt';
my $output2 = 'uncommon.txt';
open my $fhout1, ">", $output1 or die "ERROR: Can't open file '$output
+1': $!";
open my $fhout2, ">", $output2 or die "ERROR: Can't open file '$output
+2': $!";
# Compare in one direction
foreach my $line (keys %lines_found_in_file1) {
if(exists $lines_found_in_file2{$line}) {
print $fhout1 $line, "\n";
}
else {
print $fhout2 $line, "\n";
}
}
# Compare in other direction
foreach my $line (keys %lines_found_in_file2) {
if(exists $lines_found_in_file1{$line}) {
# Don't do anything as you printed these lines in the first go
}
else {
print $fhout2 $line, "\n";
}
}
close $fhout1 or die "ERROR: Couldn't close file '$output1': $!";
close $fhout2 or die "ERROR: Couldn't close file '$output2': $!";
Best regards
McA
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Outside of code tags, you may need to use entities for some characters:
| |
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.
|
|