When posting response code, it's important to demonstrate good form to those seeking wisdom. In the above code, you have a few elements which, while functional, are probably bad habits.
  1. You use a two-argument open. Three argument makes intent more obvious and keeps your script from misbehaving if your file name contains special characters. This is particularly pertinent in a web-context or when piping. See open.
  2. You do not test your opens for success. If you don't do this, you may get weird failures far from the source of the problem since the open fails silently.
  3. You declare all of your lexical variables at the head of the file, rather than keeping them as tightly scoped as possible. This can result in data leaking between between parts of your script and spooky action at a distance errors. You have essentially created global variables, and hamstrung some of the great power that strict offers you.
  4. When split is invoked with one argument, it is applied to the special variable $_, so you can omit that from your split arguments. If you are implicitly populating $_, which you are, you should leverage that power consistently. Additionally, although the OP used /\s/ in that split, the default split argument of /\s+/ is probably more appropriate - manually edited tab-delimited files often end up no longer tab-delimited. Therefore, that line should probably just be my ($key, $val) = split;.
  5. Since you are using Indirect Filehandles, there is no need to explicitly close the file. The file will be automatically closed once the filehandle goes out of scope - one of the great qualities of indirect filehandles in the first place.
All of the above points are addressed in the following rewrite of your code:

use strict; use warnings; my %data; for my $file ('file1.txt', 'file2.txt', 'file3.txt', 'file4.txt' , 'file5.txt') { open(my $handle, '<', $file) or die "Open fail on $file: $!\n"; while (<$handle>) { chomp; my ($key, $val) = split; $data{$key}{'count'}++; $data{$key}{'sum'} += $val; } } open(my $handle, '>', 'average.txt') or die "Open fail on average.txt: + $!\n"; for my $key (sort { $a <=> $b } keys %data) { $data{$key} = $data{$key}{'sum'} / $data{$key}{'count'}; print $handle "$key\t$data{$key}\n"; }

Other changes I might include would be not quoting key arguments for hashes and the use of qw for quoting the file list (see qw/STRING/).


In reply to Re^2: averages from multiple files by kennethk
in thread averages from multiple files by Taylorswift13

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.