In case the ligs names are unique, you could also use a hash as a lookup table, so you wouldn't have to compare every entry in file1 against every entry in file2.

#!/usr/bin/perl use warnings; use strict; die "type file1 and file2 and output" unless @ARGV==3; my ($fin1, $fin2, $fout) = @ARGV; open my $in1, "<", $fin1 or die "Couldn't open '$fin1': $!"; open my $in2, "<", $fin2 or die "Couldn't open '$fin2': $!"; open my $out, ">", $fout or die "Couldn't open '$fout': $!"; <$in1>; <$in2>; # skip headers my %ligs; # lookup table while (<$in1>) { chomp; my ($lig, @fields) = split /\t/; $ligs{$lig} = [@fields]; } while (<$in2>) { chomp; my ($lig, @f2) = split /\t/; if (exists $ligs{$lig}) { my $f1 = $ligs{$lig}; # get corresponding entry from other fi +le my @diffs; for (0..$#f2) { $diffs[$_] = $f1->[$_] - $f2[$_]; } print $out "$lig\t", join(" ", map sprintf("%.3f",$_), @diff +s), "\n"; } }

P.S.:

... ... }}}}}}}

Properly indenting your code would make it much better readable.  Even if it's only you maintaining the code, do yourself that favor — white space is cheap these days :)


In reply to Re: compare two data sets (matrix) by almut
in thread compare two data sets (matrix) by paola82

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.