Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Column by Column Comparison

by poj (Priest)
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"; }
poj


Comment on Re: Column by Column Comparison
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1026355]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (10)
As of 2015-07-07 22:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (93 votes), past polls