Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: compare text files

by kcott (Chancellor)
on Sep 12, 2012 at 10:28 UTC ( #993204=note: print w/replies, xml ) Need Help??


in reply to compare text files

G'day linseyr,

Welcome to the monastery.

This works with the data you've provided.

#!/usr/bin/env perl use strict; use warnings; my %least_ref; open my $reference, '<', $ARGV[0] or die "Can't open reference ($ARGV[ +0]): $!"; while (<$reference>) { my ($key, $val) = split; if (not exists $least_ref{$key} or $val < $least_ref{$key}) { $least_ref{$key} = $val; } } close $reference; open my $testfile, '<', $ARGV[1] or die "Can't open testfile ($ARGV[1] +): $!"; while (<$testfile>) { my ($key, $val) = split; print "$key\t", $least_ref{$key} - $val, "\n"; } close $testfile;

Input files:

$ cat > pm_least_diff.ref chr1 40 chr1 50 chr2 60 chr2 80 chr3 100
$ cat > pm_least_diff.test chr1 30 chr2 20 chr2 50 chr3 80

Script output:

$ pm_least_diff.pl pm_least_diff.ref pm_least_diff.test chr1 10 chr2 40 chr2 10 chr3 20

-- Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://993204]
help
Chatterbox?
[marto]: or closes the question because it's a almost the same as one someone else asked, years ago...
[marto]: snackoverflow gamification of editorial powers for the win :/
marto should register snarkoverflow
[marto]: nothing to see here people, go about your daily business :P

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2018-01-23 10:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How did you see in the new year?










    Results (243 votes). Check out past polls.

    Notices?