in reply to extracting and using values from a matrix file
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:
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.#!/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"; }
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"):
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.# pipe data from some other process: some_matrix_program | getavg # or read data from some file: getavg matrix.file
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.
|
|---|