The following approach is similar to your first pass. The entire output hash is built in this pass. Every time I see a new key, rather than initializing it with an empty array, I use an array of zeros and overwrite the one corresponding to the file. Every time I find an existing key, I overwrite the zero which corresponds to both the file and that key. The resulting hash of arrays is passed to the print section. It prints one line for each key. It corrects the format of srand then prints the corrected key and the values of the data array.
use strict;
use warnings;
use feature 'state';
my @files = glob "file*.tab";
my %output;
foreach my $file (@files) {
open my $fh, '<', $file or die $!;
state $fn = -1;
$fn++;
while (<$fh>) {
chomp;
my @s = split /\t/;
die "_ERROR_ not 9 colmns [$-]\n" if @s != 9;
my $key = join "\t", @s[0..3];
$output{$key} = [('0') x @files] if (!exists $output{$key});
$output{$key}[$fn] = $s[6];
}
close $fh;
}
print join("\t",'chr','fivep','threep','strand',@files),"\n";
foreach my $key (sort keys %output) {
my $values = $output{$key};
my $temp_key = $key;
$temp_key =~ s/[^\t]+$/ ($& eq 0) ? '+' : '-'/e;
print join( "\t", $temp_key, @$values ), "\n";
}
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.