Was this page helpful?

Practical Biocomputing 2011 Homework 1

    Table of contents
    1. 1. Hints

    Due: 13 january

    Write a script to read a file and calculate the number of lines, number of columns,  and the average value of each column.  Example input files areattached below.  Note that the first line gives the column headings and should be skipped.  Similarly, the first column gives the name of molecule and should also be omitted in both the count of columns and the average. To get the values from the individual columns

    @values = split " ", $line;    #$line is one line read from the file

    Your program should produce exactly the output in hw1_out_example.txt when you use hw1_in_example.txt as input.


    1. use a while loop to read the file on standard input (STDIN).  We haven't talked about how to use open to open files by name yet

    while ( $line = <> ) {


    2. inside the loop, increment a variable to count the lines, e.g.,


    3. use the suggested code, above, to separate the values on the line.  The result is an array, @values.  Individual values in the array are accessed with $values[0], $values[1], etc.  For the first line the values will be the names of the columns, for the other lines, they will be the name of the sequence and the sequence positions.  The number of columns is the number of elements in this array.  The number of elements is obtained by assigning the array to a scalar variable

    $n_elements = @array;

    4. Figure out how to skip the header line, e.g.,

    if ( $values[0] eq 'Sequence_id' ) {
    } else {

    5. use a series of variables to collect the running sums of the values, e.g.,

    $sum_left_begin += $values[1];
    $sum_left_end += $values[2];

    6. After the end of the while loop, divide by the number of lines to get the averages (don't forget to skip the header line), e.g.,

    $ave_left_begin = $sum_left_begin / ( $count - 1 );
    Was this page helpful?
    Tag page (Edit tags)
    • No tags
    You must login to post a comment.