For example :
File 1:
Apple
Orange
grapes
File 2:
Orange
grapes
Apple
I am trying to check whether both files have the same data in different order.
How to do it without using any external module?
For example :
File 1:
Apple
Orange
grapes
File 2:
Orange
grapes
Apple
I am trying to check whether both files have the same data in different order.
How to do it without using any external module?
If you want to use perl to find the difference between two files, you could try the Text::Diff CPAN module.
doing it manually is a simple exercise. read the first file into a hashtable of line/linenumber, then delete the second file from that table. if an entery doeant exist, put it into a second table. Anything in the tables indicates stuff that doesn't match, and the tables contain line numbers of the differing lines.
This is a simple matter of reading each line of the two files into a hash and then comparing the contents of the two hashes. Basically that is a beginner's programming exercise.
Here is an simple way to do what you want in perl:
In pfile1:
Apple
Orange
grapes
In pfile2:
Orange
grapes
Apple
the perl script:
#!/usr/bin/env perl
open (FILE1, "pfile1") || die ("Can't open file pfile1 for reading");
open (FILE2, "pfile2") || die ("Can't open file pfile2 for reading");
my @file1 = <FILE1>;
my @file2 = <FILE2>;
@sorted_file1 = sort @file1;
@sorted_file2 = sort @file2;
die("Your Files are different\n")
unless ($#sorted_file1 == $#sorted_file2);
for my $item (0 .. $#sorted_file1) {
if ($sorted_file1[$item] ne $sorted_file2[$item]) {
die("Your Files are different\n");
}
}
print "Your Files are the same\n";
This works by reading the files lines into an array, then sorting the array. It checks that both arrays are the same length, then exits early if the corresponding index value between the two arrays is different.
You will then get a message stating that the files are the same...or not.
use strict;
use warnings;
# $d{LINE} = TALLY
my ($n, %d) = (1);
while (<>){
$d{$_} += $n;
$n *= -1 if eof;
}
# Now get whatever kind of lines you are interested in.
my @same_in_both_files = grep { $d{$_} == 0 } keys %d;
my @surplus_in_file1 = grep { $d{$_} > 0 } keys %d;
my @surplus_in_file2 = grep { $d{$_} < 0 } keys %d;
# Or just get a true-false verdict.
my $files_differ = 1 if grep $_, values %d;