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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |