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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |