What I would like to do is take these values and calculate the average.

Do you mean that you just want a single numeric value as output, which is merely the sum of all the input numbers, divided by the number of input values? If so, you don't need any sort of complicated data structure inside your perl script:

#!/usr/bin/perl use strict; use warnings; my $sum = my $count = 0; while (<>) { for my $n ( split ) { $sum += $n; $count++; } } if ( $count ) { printf( "Average over %d values: %7.3\n", $count, $sum/$count ); } else { warn "No numeric data\n"; }
Note that the default operation for split is to return the list of whitespace separated tokens from $_; since newline character(s) at the end of each line of input are whitespace, split removes them.

In case your matrix file happens to contain any non-numeric tokens (e.g. words), those will be treated as zero when adding values to $sum. (update: but they will be counted in $count, increasing the divisor for the average)

The "diamond" operator in the while loop condition allows you to run the script in either of two ways (supposing the script were stored as "getavg"):

# pipe data from some other process: some_matrix_program | getavg # or read data from some file: getavg matrix.file
In the latter case, if you gave two or more matrix file names on the one command line, you'd get a single average over all files combined.

If your needs are more complicated than getting a single global average value, you'll need to explain them better. There's likely to be an easy solution.


In reply to Re: extracting and using values from a matrix file by graff
in thread extracting and using values from a matrix file by Angharad

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.