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";
}