Why do I see no computed results in my output file?

南楼画角 提交于 2019-12-04 05:49:14

问题


This is a follow-up to How do I average column values from a tab-separated data file, ignoring a header row and the left column?. The task was: open and read a file; reach each line, split the contents into an array, and compute the average of the numerical values; and finally to write to a new file the averages for each of the columns containing numerical values.

All seems to be well up until the final point. The problem is, though I can create a new .txt file, the .txt file itself doesn't have what's printed in the output. Preferably, as a new user of Perl, I'd prefer to keep the script in the style written below so I can understand it better. I'm not so good with the more succinct versions that could potentially be out there. Thanks to jchips12 for being considerably helpful.

Anyway, the code is:

#!/usr/bin/perl -w
use strict;
my $infile = "Lab1_table.txt"; # This is the file path
open INFILE, $infile or die "Can't open $infile: $!";
my $outfile = "Lab1_tableoutput.txt";
open OUTFILE, ">$outfile" or die "Cannot open $outfile: $!";

my $count = 0;
my @header = ();
my @average = ();

while (<INFILE>) {
    chomp;
    my @columns = split /\t/;
    $count++;
    if ( $count == 1 ) {
        @header = @columns;
    } else {
        for( my $i = 1; $i < scalar @columns; $i++ ) {
            $average[$i] += $columns[$i];
        }
    }
}

for( my $i = 1; $i < scalar @average; $i++ ) {
    print $average[$i]/($count-1), "\n";
}

print OUTFILE "\n";
close OUTFILE;

The data came from the file Lab1_table.txt as follows:

retrovirus      genome  gag     pol     env
HIV-1           9181    1503    3006    2571
FIV             9474    1353    2993    2571
KoRV            8431    1566    3384    1980
GaLV            8088    1563    3498    2058
PERV            8072    1560    3621    1532

The result produces the correct average values, albeit a bit messy in the terminal and they aren't labelled corresponding to any column number/name as such. Also, a .txt file gets produced, but with no output.

The results came out as:

Argument "" isn't numeric in addition (+) at line 25, <INFILE> line X
0
8649.2
1509
3300.4
2142.4

***Line X: Where X is either 2, 3, 4, 5, or 6.***

From this I can infer that the “Argument” errors are referring to the 5 header columns, and the 0 to the only column with non-numerical values.

Help with getting the file to write to a .txt file, or in someway which I can read the output as shown in command line would be much appreciated. Also, though I know vaguely what's going on at each step of the code, I'd appreciate some more insight into what's going on in most of the steps, if possible. I'm still reading it up, but the more fine details I want to be able to understand clearly.


回答1:


Specifying comments for each line to give you clear understanding

#!/usr/bin/perl -w 
use strict; 
use warnings;

my $infile = "Lab1_table.txt";                         # input file path 
open INFILE, $infile or die "Can't open $infile: $!";  # input file opened
my $outfile = "Lab1_tableoutput.txt";                  # output file path
open OUTFILE, ">$outfile" or die "Cannot open $outfile: $!"; # output file opened

my $count = 0;              # count variable to check for header row in file 
my @header = ();            # variable to store headers/column names of file
my @average = ();           # variable to store average calculated for each column

while (<INFILE>) {    
 chomp;
 my @columns = split /\s+/;   # \s stands for  [\ \t\r\n\f]
 $count++;    

 if ( $count == 1 ) {         
                    @header = @columns;      # executed only once for header 
      } 
 else {                                       # else column executed for remaining rows
        for( my $i = 1; $i < scalar @columns; $i++ ) {  # $i=1 means skip first column
              $average[$i] += $columns[$i];      # calcuate average for each row   
          }
      }
} 
for( my $i = 1; $i < scalar @average; $i++ ) {     

    print OUTFILE $average[$i]/($count-1), "\n";  # This will write to output file

    }     
close OUTFILE; 

Use print OUTFILE $average[$i]/($count-1), "\n"; to write to file.

Error Argument "" isn't numeric in addition (+) at line 25, <INFILE> line X could be if values in columns which you are adding,by any chance has string in it and not number.Check your input file.

Note: I am not getting above error.Script runs smooth with above data.However if I change one of number to string,I get this error.



来源:https://stackoverflow.com/questions/9716629/why-do-i-see-no-computed-results-in-my-output-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!