Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy do I see no computed results in my output file?
    primarykey
    data
    text
    <p>This is a follow-up to <a href="https://stackoverflow.com/questions/9677533/how-do-i-average-column-values-from-a-tab-separated-data-file-ignoring-a-header">How do I average column values from a tab-separated data file, ignoring a header row and the left column?</a>. 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.</p> <p>All seems to be well up until the final point. The problem is, though I can create a new <code>.txt</code> file, the <code>.txt</code> 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 <a href="https://stackoverflow.com/users/882628/jchips12">jchips12</a> for being considerably helpful.</p> <p>Anyway, the code is:</p> <pre><code>#!/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, "&gt;$outfile" or die "Cannot open $outfile: $!"; my $count = 0; my @header = (); my @average = (); while (&lt;INFILE&gt;) { chomp; my @columns = split /\t/; $count++; if ( $count == 1 ) { @header = @columns; } else { for( my $i = 1; $i &lt; scalar @columns; $i++ ) { $average[$i] += $columns[$i]; } } } for( my $i = 1; $i &lt; scalar @average; $i++ ) { print $average[$i]/($count-1), "\n"; } print OUTFILE "\n"; close OUTFILE; </code></pre> <p>The data came from the file <code>Lab1_table.txt</code> as follows:</p> <pre><code>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 </code></pre> <p>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 <code>.txt</code> file gets produced, but with no output.</p> <p>The results came out as:</p> <pre><code>Argument "" isn't numeric in addition (+) at line 25, &lt;INFILE&gt; line X 0 8649.2 1509 3300.4 2142.4 ***Line X: Where X is either 2, 3, 4, 5, or 6.*** </code></pre> <p>From this I can infer that the “Argument” errors are referring to the 5 header columns, and the <code>0</code> to the only column with non-numerical values.</p> <p>Help with getting the file to write to a <code>.txt</code> 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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload