rhew.org

Algorithm::Diff - Source Code

#!/usr/bin/perl
use strict;
use Algorithm::Diff;

# user arguments
my (@fileList1, @fileList2);
my ($file1, $file2)=(shift(), shift()); 
die "You must specify 2 files" if not defined $file1 or not defined $file2;

sub hashKey
{
    return $1 if (shift() =~ /([^ ]*)/);
}

sub hashValue
{
    return $1 if (shift() =~ /[^ ]* (.*)/);
}

open (FILE1, "<$file1") or die "could not open $file1";
while (<FILE1>)
{
    chomp;
    push(@fileList1, $_);
}

open (FILE2, "<$file2") or die "could not open $file2";
while (<FILE2>)
{
    chomp;
    push(@fileList2, $_);
}

print "TN         File 1   File 2\n".
      "---------- -------- ---------\n";

Algorithm::Diff::traverse_sequences( \@fileList1, \@fileList2, {

    MATCH     => sub{    my ($a, $b)=(shift(), shift());
                         print hashKey  ($fileList1[$a])." ".
                               hashValue($fileList1[$a])." ".
                               hashValue($fileList2[$b])." "."\n";},

    DISCARD_A => sub{    my ($a, $b)=(shift(), shift());
                         print hashKey  ($fileList1[$a])." ".
                               hashValue($fileList1[$a])." ".
                               "--:--:--"."\n";},

    DISCARD_B => sub{    my ($a, $b)=(shift(), shift());
                         print hashKey($fileList2[$b])." ".
                               "--:--:-- ".
                               hashValue($fileList2[$b])." "."\n";}},
    \&hashKey);

rhew.org (c) 2001 James Rhew