I don't see why files would be included twice. Why don't you start out by printing the content of @files to see if it contains duplicates?

Also, your script won't compile under strictures. And it uses bare filehandles instead of lexically scoped ones. And it does that without ever closing any files after use. It also never checks to be sure that a file got properly opened.

I've taken a minute or two to alter your code just enough to get it to pass strictures, and to explicitly close filehandles. I've also used lexical filehandles, which actually would close themselves when they fall out of scope, so my 'close' lines are somewhat redundant. I've also added the classical "or die..." mechanism to your file opens, so that if a file is failing to open you'll know about it.

Here's the code...

use strict; use warnings; my @files = grep /^output\d+$/, <output*>; my ( $total, $i ); foreach my $file (@files) { open ( my $in, "<$file" ) or die "Couldn't open $file: $!\n"; while ( my $input = <$in>) { print "$file\n"; my ($x, $y, $z, $sum) = split (/\s+/, $input); $total += $sum; $i++; my $div = $total/$i; my $sqrt = sqrt($div); print "$total\n"; open ( my $out2, ">RMSD$file") or die "Couldn't open RMSD$file: $!\n"; print $out2 $sqrt; close $out2; } close $in; }

Update: I missed the mark on the actual problem, and others were correct in finding that you're printing to the output file once per line. You're also opening the output file once per line... it hasn't been mentioned, but each time you open the file for output in the way you are, it clobbers whatever the file contained previously. That's kinda a mess. ...my example didn't fix that either, but it's certanly worth mentioning.


Dave


In reply to Re: files stored in an array by davido
in thread files stored in an array by Anonymous Monk

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.