Mailing List Archive

Support open source code!


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: tlug: Horizontal diff?



Simon Cozens (lists.tlug):
>Sounds fun. Here's a fairly bad-but-correct way of doing it in Perl.

Sorry, I wasn't thinking at all. Here's a shorter and much, much, much,
much better solution. Requires the Algorithm::Diff module from CPAN
(or, better, from
http://othersideofthe.earth.li/Algorithm-Diff-0.59.tar.gz which, despite
the name, is in my bedroom in Jiyugaoka.)

#!/usr/bin/perl -w
use strict;
open (ONE, $ARGV[0]) or die $!; open (TWO, $ARGV[1]) or die $!;
use Algorithm::Diff qw(diff);
my $line=0;
while (++$line) {
    my ($one,$two) = (scalar <ONE>,scalar <TWO>);
    last unless defined $one and defined $two; next if $one eq $two;
    my @example.com = split //,$one; my @example.com = split //,$two;
    print "\nLine $line differs.\n";
    for(@{ diff(\@example.com,\@example.com)}) {
        print "At position ".$_->[0]->[1].":\n";
        my $out=join "", map {$_->[2]} grep {$_->[0] eq "-"} @$_;
        my $in=join "", map {$_->[2]} grep {$_->[0] eq "+"} @$_;
        print "Remove:\t'$out' \n" if $out;
        print "Add:\t'$in' \n" if $in;
    }
}

>It has *no* smartness at all; if you have lines:
>foo123456
>foob123456

This has smartness coming out of its ears.

Line 1 differs.
At position 3:
Add:    'b'

>Given a file "one":
>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaafhfhfhfasdddddddddddaaaaaaaaaaaaaaaaaaaaaaas
>bbbbbbbbbbbbbbbbbbbbbbbbbbbbfeefehfbbbbbbbbbbbbbbbbbbbbbasdasdadsbabdbfefbbb
>
>And a file "two":
>aaaaaasdddsdsaaaaaaaaaaaaaaaaaafhfhfhfasdddddvddddddaaaaaaaaaaaaaaaaaaaaaaas
>bbbbbbbbbbbbbbbbbbbbbbbbbbbbfeefehfbbbbbbbbb  bbbbbbbbbbasdasdadsbabdbfefbbb

Line 1 differs.
At position 6:
Remove: 'aaaaaaaa'
Add:    'sdddsds'
At position 45:
Add:    'v'

Line 2 differs.
At position 44:
Remove: 'bb'
Add:    '  '

>Next Nomikai Meeting: February 18 (Fri) 19:00 Tengu TokyoEkiMae

Ooh, is it too late to sign up?

-- 
"Contrariwise," continued Tweedledee, "if it was so, it might be, and
if it were so, it would be; but as it isn't, it ain't.  That's logic!"
		-- Lewis Carroll, "Through the Looking Glass"
--------------------------------------------------------------------
Next Nomikai Meeting: February 18 (Fri) 19:00 Tengu TokyoEkiMae
Next Technical Meeting:  March 11 (Sat) 13:00 Temple University Japan
* Topic: TBD
--------------------------------------------------------------------
more info: http://www.tlug.gr.jp        Sponsor: Global Online Japan


Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links