Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: Column by Column Comparison

by poj (Prior)
on Mar 31, 2013 at 13:40 UTC ( #1026355=note: print w/replies, xml ) Need Help??

in reply to Column by Column Comparison

Using a hash approach
use strict; my %data=(); scan (0,'File1.txt'); scan (1,'File2.txt'); compare('log.txt'); # input sub scan { my ($ix, $infile) = @_; open IN, '<', $infile or die "Could not open $infile : $!"; my $count=0; while (<IN>){ chomp; my ($key,$line) = split "\t",$_,2; $data{$key}[$ix] = $line; ++$count; } close IN; print "$count lines read from $infile\n"; } # output sub compare { my $logfile = shift; open LOG,'>',$logfile or die "Could not open $logfile : $!"; my $count = 0; # compare lines of data using key for my $key (sort keys %data){ if ($data{$key}[0] ne $data{$key}[1]){ my @f1 = split "\t",$data{$key}[0]; my @f2 = split "\t",$data{$key}[1]; for my $c (1..@f1){ if ($f1[$c-1] ne $f2[$c-1]){ print LOG "Row $key Column $c File 1 Value- $f1[$c-1] File 2 + Value- $f2[$c-1]\n"; ++$count; } } } } close LOG; print "$count lines written to $logfile\n"; }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1026355]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2017-06-26 22:02 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (594 votes). Check out past polls.