Debian Codegraph @

average

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