ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/gclib/scripts/col_calc.pl
Revision: 24
Committed: Tue Jul 26 21:46:39 2011 UTC (8 years, 2 months ago) by gpertea
File size: 844 byte(s)
Log Message:
Line User Rev File contents
1 gpertea 23 #!/usr/bin/perl
2     use strict;
3     use Getopt::Std;
4     use FindBin;use lib $FindBin::Bin;
5    
6     my $usage = q/Usage:
7     col_calc.pl [-f <col#>] <tab_delimited_data>..
8    
9     Compute sum and average values for the specified numeric column
10     of the input data.
11     /;
12     umask 0002;
13     getopts('f:o:') || die($usage."\n");
14     my $outfile=$Getopt::Std::opt_o;
15     my $f=$Getopt::Std::opt_f || 1;
16     $f-- if $f>0;
17     if ($outfile) {
18     open(OUTF, '>'.$outfile) || die("Error creating output file $outfile\n");
19     select(OUTF);
20     }
21     # --
22     my ($n,$t);
23     while(<>) {
24     chomp;
25     next unless $_;
26     my @d=split(/\t/);
27     unless ($d[$f]=~m/^[\d\.\-\+]+$/) {
28     print STDERR "Warning: ignored non-numeric field: $d[$f]\n";
29     next;
30     }
31     $t+=$d[$f];
32     $n++;
33     }
34     print "Sum of $n values is $t, average=".($t/$n)."\n";
35    
36     # --
37     if ($outfile) {
38     select(STDOUT);
39     close(OUTF);
40     }
41    
42     #************ Subroutines **************

Properties

Name Value
svn:executable *