Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I average column values from a tab-separated data file, ignoring a header row and the left column?
    primarykey
    data
    text
    <p>First and foremost I apologise if this, or a similar query, has been posted before but I did follow the steps and looked here and beyond and that is why I'm resorting to asking a question, something I rarely ever do.</p> <p>Background before my query - I'm a final year biomedical student, and I decided to take a Bioinformatics paper - a paper that has only begun to be offered at my university this year. I thought it would be a good change, but now that I've had experience two weeks in, I'm not finding it particularly engaging. Challenging, yes, but not engaging, in that I've never done any programming in my life, and I'm expecting to learn this so suddenly. As such, anything I present to you is as a complete 'newbie', and I admit that I literally have little to no clue on how to go about doing things and I really do want to try and learn.</p> <p>Anyway, onto my query…</p> <p>My task is to compute averages from the following data file, titled <code>Lab1_table.txt</code>:</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>I have to write a script that will open and read this file, read each line by splitting the contents into an array and computer the average of the numerical values (<code>genome</code>, <code>gag</code>, <code>pol</code>, <code>env</code>), and write to a new file the average from each of the aforementioned columns.</p> <p>I've been trying my best to figure out how to not take into account the first row, or the first column, but every time I try to execute on the command line I keep coming up with 'explicit package name' errors.</p> <pre><code>Global symbol @average requires explicit package name at line 23. Global symbol @average requires explicit package name at line 29. Execution aborted due to compilation errors. </code></pre> <p>I understand that this involves <code>@</code> and <code>$</code>, but even knowing that I've not been able to change the errors.</p> <p>This is my code, but I emphasise that I'm a beginner having started this just last week:</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 $count = 0; my $average = (); while (&lt;INFILE&gt;) { chomp; my @columns = split /\t/; $count++; if ( $count == 1 ) { $average = @columns; } else { for( my $i = 1; $i &lt; scalar $average; $i++ ) { $average[$i] += $columns[$i]; } } } for( my $i = 1; $i &lt; scalar $average; $i++ ) { print $average[$i]/$count, "\n"; } </code></pre> <p>I'd appreciate any insight, and I would also great appreciate letting me know by list numbering what you're doing at each step - if appropriate. I'd like to learn and it would make more sense to me if I was able to read through what someone's process was.</p>
    singulars
    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