#!/usr/bin/perl -w # # addup: Add up numbers that are tokens of each line, until EOF. # # 2006-??-??: Written by Steven J. DeRose, sderose@acm.org. # 2006-11-28 sjd: Fix missing \\ in default $delim. # 2007-09-26 sjd: Add -avg. # 2008-08-31 sjd: Add -n *, -product, use getOpt, output multiple bases. # # To do: use strict; use Getopt::Long; my $version = "2008-08-31"; my $avg = 0; my $bases = 0; my $delim = "\\s+"; my $e = ""; # error-message prefix my $fieldNum = 1; my $help = 0; my $product = 0; my $quiet = 0; my $verbose = 0; ############################################################################### Getopt::Long::Configure ("ignore_case"); my $result = GetOptions( "avg!" => \$avg, "b!" => \$bases, "d=s" => \$delim, "h|help" => \$help, "n=s" => \$fieldNum, "product!" => \$product, "q!" => \$quiet, "v+" => \$verbose, "version" => sub { die "Version of $version by Steven J. DeRose, sderose\@acm.org.\n"; } ); if ($help) { showUsage(); exit; } ($result) || die "Bad options.\n"; ############################################################################### # Set implied options, validate option values... (!$ARGV[0]) || die "Wrong number of arguments.\n"; if ($fieldNum ne "*") { $fieldNum -= 0; ($fieldNum > 0) || die "Bad value for -n fieldnumber option.\n"; } ############################################################################### my $total = 0; my $totalProduct = 1; my $notenough = 0; my $records = 0; while (<>) { $records++; my $this = $_; chomp($this); $this =~ s/^\s*//; my @tokens = split(/$delim/, $this); if ($fieldNum eq "*") { # all tokens for (my $i=0; $i