Debian Codegraph @

average

raw ◀ ▶
Source reconstructed at patch state 2 of 4 — not the installed (fully patched) source.
4 Debian patches touch this file
1 #!/usr/bin/perl -w
2
3 # numaverage: Find the average of a set of numbers.
4 #
5 # Copyright (C) 2002-2004 Suso Banderas
6
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License
9 # as published by the Free Software Foundation; either version 2
10 # of the License, or (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #
21 # You may contact the author at <[email protected]>.
22
23 #######################
24 # VARIABLES AND SETUP #
25 #######################
26
27 use Getopt::Std;
28 use strict;
29 use vars qw/ %opts $verbose /;
30
31 getopts('dhiIlmMqV', \%opts);
32
33 if ($opts{'h'}) {
34 &help();
35 exit(0);
36 }
37
38 if ($opts{'d'}) {
39 $verbose = 3;
40 print STDERR "Debug mode\n";
41 } elsif ($opts{'V'}) {
42 $verbose = 2;
43 print STDERR "Verbose mode\n";
44 } elsif ($opts{'q'}) {
45 $verbose = 0; # Nothing except the final answer
46 } else {
47 $verbose = 1; # Normal warnings and such.
48 }
49
50 my ($file, @number_array, $number, $total_numbers, @numbers, $strlen);
51 my $sum_total = 0;
52
53 ################
54 # MAIN PROGRAM #
55 ################
56
57 # For file args
58 if (@ARGV) {
59 foreach $file (@ARGV) {
60 print STDERR "Reading from file $file.\n" if ($verbose >= 2);
61 open (ARGFILE, "$file") &&
62 process_filehandle(\*ARGFILE, \@number_array) ||
63 $verbose && warn "Couldn't open file $file for reading: $!\n";
64 close (ARGFILE);
65 }
66 # For STDIN
67 } else {
68 print STDERR "Reading from STDIN.\n" if ($verbose >= 2);
69 process_filehandle(\*STDIN, \@number_array);
70 }
71
72 my $average;
73 if ($opts{'m'}) {
74 $average = find_mode(\@number_array);
75 } elsif ($opts{'M'}) {
76 $average = find_median(\@number_array);
77 } else {
78 $average = calculate_mean(\@number_array);
79 }
80
81 # post processing.
82 if ($opts{'i'}) {
83 $average = int($average);
84 } elsif ($opts{'I'}) {
85 if ($average == int($average)) {
86 $average = 0;
87 } else {
88 $average =~ s/^(\-?)[0-9]*\.([0-9]*)$/$1.$2/;
89 }
90 }
91
92 print "$average\n";
93
94
95 ###############
96 # SUBROUTINES #
97 ###############
98
99 sub help {
100 print <<"EOF";
101 ---------------------------------------------------------
102 numaverage : A program for finding the average of numbers.
103 ---------------------------------------------------------
104 Usage:
105 numaverage [options] file
106 | numaverage [options]
107 numaverage [options]
108
109 Options:
110 -i Only return the integer portion of the final sum.
111 -I Only return the decimal portion of the final sum
112
113 -m Find the mode (most occuring) of the list of numbers,
114 or when there's more than one mode, the first completed mode.
115 -M Find the median (middle number) of the list of numbers.
116 -l When finding the median and the count of numbers in the set is even,
117 use the lower middle number instead of the upper middle number.
118
119 -d Debug. For developers only.
120 -h Help: You're looking at it.
121 -V Increase verbosity.
122 -q Quiet mode, don't print any warnings.
123 EOF
124 }
125
126 sub process_filehandle {
127 my $filehandle = shift;
128 my $number_array_ref = shift;
129
130 while (<$filehandle>) {
131 if (m/^\s*(\-?[0-9]*\.?[0-9]+)/) {
132 print STDERR "found number: $1\n" if ($verbose >= 3);
133 push(@$number_array_ref, $1);
134 }
135 }
136 return 1;
137 }
138
139 sub calculate_mean {
140 my $number_array_ref = shift;
141
142 my $number_of_numbers = @$number_array_ref;
143 my $runningtotal = 0;
144 foreach $number (@$number_array_ref) {
145 $runningtotal += $number;
146 print STDERR "adding number: $number (total: $runningtotal)\n" if ($verbose >= 3);
147 }
148 if( $number_of_numbers <= 0 )
149 {
150 print STDERR "Empty list of numbers\n";
151 &help();
152 exit(-1);
153 }
154 $average = $runningtotal / $number_of_numbers;
155 print STDERR "averaging numbers: $runningtotal total / $number_of_numbers numbers = $average\n" if ($verbose >= 2);
156 return $average;
157 }
158
159 sub find_mode {
160 my $number_array_ref = shift;
161 my (%numbers_count,$highest_number,$highest_count);
162 foreach $number (@$number_array_ref) {
163 $numbers_count{$number}++;
164 if (!defined($highest_number)) {
165 $highest_number = $number;
166 $highest_count = 1;
167 } elsif ($numbers_count{$number} > $highest_count) {
168 $highest_number = $number;
169 $highest_count++;
170 print STDERR "Highest number is now: $highest_number with a count of $highest_count.\n" if ($verbose >= 2);
171 }
172 }
173 return $highest_number;
174 }
175
176 # This routine will find the median of a set of numbers.
177 # If the set of numbers counts to be even, then it will take
178 # the higher indexed number in the middle of the set.
179 # If the -l option is set, it will take the lower index
180 # number in the middle.
181 sub find_median {
182 my $number_array_ref = shift;
183 my @number_array = sort {$a <=> $b} @$number_array_ref;
184 my $array_count = scalar(@number_array);
185 my $median_index;
186 print "The array count is: $array_count\n" if ($verbose >= 2);
187 if (($array_count % 2) == 0) { # The index count is even.
188 if ($opts{'l'}) {
189 $median_index = int($array_count / 2);
190 } else {
191 $median_index = int(($array_count / 2) + 1);
192 }
193 } else {
194 $median_index = int(($array_count / 2) + 0.5);
195 }
196 $median_index--; # Translate the number to 0 based array set.
197 print "The median index is: " . int(($array_count / 2) - 0.5) . "\n" if ($verbose >= 2);
198 my $median = $number_array[$median_index];
199 return $median;
200 }
201
202 # Lay down some of that perl pod action.
203 =pod
204
205 =head1 NAME
206
207 numaverage - Find the average of a set of numbers.
208
209 =head1 SYNOPSIS
210
211 B<numaverage> [-dhiIlmMV] <FILE>
212
213 | B<numaverage> [-dhiIlmMV] (Input on STDIN from pipeline.)
214
215 B<numaverage> [-dhiIlmMV] (Input on STDIN. Use Ctrl-D to stop.)
216
217 =head1 DESCRIPTION
218
219 By default
220 B<numaverage> will determine the average from all numbers on input. Other
221 options allow you to find the mode and median.
222
223 =head1 OPTIONS
224
225 -i Only return the integer portion of the final sum.
226 -I Only return the decimal portion of the final sum
227
228 -m Find the mode (most occurring) of the list of numbers,
229 or when there's more than one mode, the first completed mode.
230 -M Find the median (middle number) of the list of numbers.
231 -l When finding the median and the count of numbers in the set is even,
232 use the lower middle number instead of the upper middle number.
233
234 -h Help: You're looking at it.
235 -V Increase verbosity.
236 -d Debug mode. For developers
237
238
239 =head1 SEE ALSO
240
241 numbound(1), numinterval(1), numnormalize(1), numgrep(1), numprocess(1), numsum(1), numrandom(1), numrange(1), numround(1)
242
243 =head1 COPYRIGHT
244
245 numaverage is part of the num-utils package, which is copyrighted by
246 Suso Banderas and released under the GPL license. Please read
247 the COPYING and LICENSE files that came with the num-utils package
248
249 Developers can read the GOALS file and contact me about providing
250 submitions or help for the project.
251
252 =head1 MORE INFO
253
254 More info on numaverage can be found at:
255
256 =over 1
257
258 =item B<http://suso.suso.org/programs/num-utils/>
259
260 =back
261
262 =cut
263