jagdish1206 has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I am trying to write a perl program that compares 2 files and writes the difference to the output file as, Example File1 aa bb ac ad File2 bb cc cd Resultant File cc cd Resultant file should contains only those records that are present in second file but not in the first file. bb is present in File1. But cc and cd of file2 are no where present in file1. Please suggest what is the best way to implement this. I am thinking of the following approach Read files into arrays(one line as an array element), sort them. Loop thru all the line of file2 check if the file is not present int file1. But it doesnt seems to be like good approch. Please suggest on this. Please provide any link or a sample snippet. Thanks in advance.

Replies are listed 'Best First'.
Re: File Comparision
by Khen1950fx (Canon) on Aug 11, 2011 at 22:42 UTC
    That's easy with List::Compare::Functional:
    #!/usr/bin/perl -sl use strict; use warnings; use List::Compare::Functional qw(:main); my @Llist = qw(aa bb ac ad); my @Rlist = qw(bb cc cd); my @Ronly = get_complement( [\@Llist, \@Rlist] ); print "@Ronly";
      thanks for the reply. could you let me know if there is any routine for doing the range wise serach. range - 20 => check for record n of file2 in { n-k, n+k } range of file1. Thanks.
Re: File Comparision
by onelesd (Pilgrim) on Aug 11, 2011 at 22:44 UTC
    If this is not a perl homework assignment, see comm.

      and/or diff. For Perl there's also many diff modules.