为什么在输出文件中看不到计算结果? [英] Why do I see no computed results in my output file?

查看:73
本文介绍了为什么在输出文件中看不到计算结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是对

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.

一切似乎都顺利到了最后一点.问题是,尽管我可以创建一个新的.txt文件,但是.txt文件本身没有在输出中打印出什么内容.最好是,作为Perl的新用户,我希望将脚本保持为下面编写的样式,以便更好地理解它.我不太可能使用更简洁的版本.感谢 jchips12 的帮助.

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.

无论如何,代码是:

#!/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;

数据来自文件Lab1_table.txt,如下所示:

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

结果产生正确的平均值,尽管在终端中有些混乱,并且它们没有相应地标记为任何列号/名称.此外,还会生成一个.txt文件,但没有输出.

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.

结果显示为:

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.***

据此,我可以推断出自变量"错误是指5个标题列,而0是唯一具有非数字值的列.

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.

帮助获取将文件写入.txt文件的方法,或者以某种方式可以如命令行所示读取输出的方法,将不胜感激.另外,尽管我不清楚地知道代码的每个步骤都在做什么,但如果可能的话,我希望对大多数步骤中的操作有更多的了解.我仍在阅读,但我想能够更清楚地了解更多细节.

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.

推荐答案

为每行指定注释以使您清楚理解

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; 

使用print OUTFILE $average[$i]/($count-1), "\n";写入文件.

错误Argument "" isn't numeric in addition (+) at line 25, <INFILE> line X可能是如果您要添加的列中的值有可能是字符串而不是数字.请检查您的输入文件.

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.

这篇关于为什么在输出文件中看不到计算结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆