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

Please help, I am trying to compare 2 files to output: - What is in the second file that is not in the first - What is in the first file but not in the second. - What are the duplicates between the 2 files The below is all I have but it is proving damn difficult. Any help would be great!
#!/usr/bin/perl -w #use strict; $file1= "StatusRegs.txt"; $file2 = "RegsUNQ.txt"; open (ONE, $file1) or die "Couldn't open $file1: $!\n"; open (TWO, $file2) or die "Couldn't open $file2: $!\n"; while ($line1 = <ONE>) { $line2 = <TWO>; if ($line1 ne $line2) { print "$line1 does not equal $line2 at line $.\n"; } else { print "$line1 is the same in both files at line $.\n"; } }

Replies are listed 'Best First'.
Re: Comparing 2 files without diff or compare
by Zaxo (Archbishop) on May 02, 2007 at 01:49 UTC
    use Algorithm::Diff;

    Algorithm::Diff is a perl implementation of system diff. The presence of unix utilities is unnecessary and no fork is needed.

    After Compline,
    Zaxo

Re: Comparing 2 files without diff or compare
by merlyn (Sage) on May 02, 2007 at 06:59 UTC
    Untested, but it usually works, and written in a way that if this is homework, someone will notice.
    sub slurp { local *ARGV; @ARGV = @_; <> } my %where; $where{$_} .= "1" for slurp($file1); $where{$_} .= "2" for slurp($file2); for (sort keys %where) { my $where = $where{$_}; if ($where =~ /12/) { print "both"; } elsif ($where =~ /1/) { print "left"; } else { print "right"; } print ":\t$_"; }

    update: Yeah, oops, thanks akho. Fixed.
      Doesn't always work, actually. What if $where eq '11111222'?
Re: Comparing 2 files without diff or compare
by ilottl (Novice) on May 02, 2007 at 02:12 UTC
    Unfortunately that module is not available on the XP system. Error: Can't locate Algorithm/Diff.pm in @INC contains...
        Got it installed just got to figure out how to use it.