You are getting there, but the way you were reading, chomping and splitting all at once caused a few problems, safer to break it up. Also, you weren't closing each file within the loop. Try this:
for my $file (@files) { my $ok = open (my $fh, "<", "$dir/$file.txt"); if (not $ok) { warn "Couldn't open file '$dir/$file.txt' ($!)\n"; next; } while (my $line = <$fh> ) { chomp $line; my @fields = split("\t",$line); my $key = $hash{"$fields[0].$fields[1]"} or next; $result{$key}{$file} += $fields[3]; } close $fh; } my $hline = join("\t",@files); print qq|\t$hline\n|; for my $key ( sort keys %result ) { my $amt = $result{$key}; my @cols = map { $amt->{$_} ? sprintf("%.1f",$amt->{$_}) : 'n/a' } + @files; my $line = join("\t",@cols); print qq|$key\t$line\n|; }
Output:
file_1 file_2 file_3 file_4 AB 1.0 4.0 4.0 4.0 AB-1 2.0 6.0 6.0 6.0 AB-4 1.0 9.0 9.0 9.0 AB-6 2.0 8.0 4.0 4.0

In reply to Re: Perl - Hashes by tangent
in thread Perl - Hashes by Perlseeker_1

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.