#!/usr/bin/perl # # compavg.pl [,...] # computes the average of column 1 # in the specified files, ignoring lines starting with # # # Author: Austin Parker # This code in the public domain -- feel free to modify to your hearts content. # If you find a bug, tell me. # # The author makes no guarantees about the quality or effectiveness of this # code, and bears no responsibility should anything go wrong. # $printOnlyCor = 0; $c1 = shift @ARGV or die "usage: compavg.pl [,...]"; if ($c1 =~ "-p") { $printOnlyCor = 1; $c1 = shift @ARGV or die "usage: compavg.pl [,...]"; } $avg = 0; $n = 0; foreach $file (@ARGV) { print "\nWorking on file $file\n" if ($printOnlyCor == 0); open IN, "<$file"; while () { next if /^#/; # get x and y. @vals = split /\s+/; $x = $vals[$c1]; next unless $x=~/^[\.\d]*\s*$/; $n++; $avg += $x; } close IN; } $avg /= $n if $n > 0; if ($printOnlyCor == 0) { print "\nAverage is $avg for $n samples\n"; } else { print "$avg"; }