Hi all,
I am new to perl and this forum.
Since I have two files:
file1 contain two lines:
mahesh
bharti
file2 contain two lines:
mahesh
orange
as you can see difference is bharti and orange. Somehow i am able to print the difference. But my requirement is it should print as below.
Difference of file as below:
File1 contain: bharti
FIle2 contain: orange
my current code written in perl as below
============================================
use strict;
use warnings;
my $f1 = "C:\\Documents and Settings\\KXDC7647\\Desktop\\file1.txt";
my $f2 = "C:\\Documents and Settings\\KXDC7647\\Desktop\\file2.txt";
my %results;
# CONSTRUCTING HASH ARRAY BY CONTENTS OF FILE1
# IT WILL IGNORE LINE IF IT CONTAINS WHITE SPACE AT BEGINNING
open FILE1, "$f1" or die "Could not open file: $! \n";
while(my $line = <FILE1>){
if ($line=~m/^\s*$/) {
next;
}
else {
$results{$line}=1;
}
}
# CONSTRUCTING HASH ARRAY BY CONTENTS OF FILE2
open FILE2, "$f2" or die "Could not open file: $! \n";
while(my $line =<FILE2>) {
if ($line=~m/^\s*$/) {
next;
}
else {
$results{$line}++;
}
}
close(FILE1);
close(FILE2);
# PRINTING THE DIFFERENCE IN PERL
foreach my $content (keys (%results)) {
print $content if values (%results)==1 ;
}
foreach my $line (keys %results) {
print $line if $results{$line} == 1;
}
===================================
It prints :
bharti
orange
Thanks in advance.
Mahesh