| 1 |
#!/usr/bin/perl -w |
| 2 |
|
| 3 |
# average: 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 |
average : A program for finding the average of numbers. |
| 103 |
--------------------------------------------------------- |
| 104 |
Usage: |
| 105 |
average [options] file |
| 106 |
| average [options] |
| 107 |
average [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 |
-M Find the median (middle number) of the list of numbers. |
| 115 |
-l When finding the median and the count of numbers in the set is even, |
| 116 |
use the lower middle number instead of the upper middle number. |
| 117 |
|
| 118 |
-d Debug. For developers only. |
| 119 |
-h Help: You're looking at it. |
| 120 |
-V Increase verbosity. |
| 121 |
-q Quiet mode, don't print any warnings. |
| 122 |
EOF |
| 123 |
} |
| 124 |
|
| 125 |
sub process_filehandle { |
| 126 |
my $filehandle = shift; |
| 127 |
my $number_array_ref = shift; |
| 128 |
|
| 129 |
while (<$filehandle>) { |
| 130 |
if (m/^\s*(\-?[0-9]*\.?[0-9]+)/) { |
| 131 |
print STDERR "found number: $1\n" if ($verbose >= 3); |
| 132 |
push(@$number_array_ref, $1); |
| 133 |
} |
| 134 |
} |
| 135 |
return 1; |
| 136 |
} |
| 137 |
|
| 138 |
sub calculate_mean { |
| 139 |
my $number_array_ref = shift; |
| 140 |
|
| 141 |
my $number_of_numbers = @$number_array_ref; |
| 142 |
my $runningtotal = 0; |
| 143 |
foreach $number (@$number_array_ref) { |
| 144 |
$runningtotal += $number; |
| 145 |
print STDERR "adding number: $number (total: $runningtotal)\n" if ($verbose >= 3); |
| 146 |
} |
| 147 |
$average = $runningtotal / $number_of_numbers; |
| 148 |
print STDERR "averaging numbers: $runningtotal total / $number_of_numbers numbers = $average\n" if ($verbose >= 2); |
| 149 |
return $average; |
| 150 |
} |
| 151 |
|
| 152 |
sub find_mode { |
| 153 |
my $number_array_ref = shift; |
| 154 |
my (%numbers_count,$highest_number,$highest_count); |
| 155 |
foreach $number (@$number_array_ref) { |
| 156 |
$numbers_count{$number}++; |
| 157 |
if (!defined($highest_number)) { |
| 158 |
$highest_number = $number; |
| 159 |
$highest_count = 1; |
| 160 |
} elsif ($numbers_count{$number} > $highest_count) { |
| 161 |
$highest_number = $number; |
| 162 |
$highest_count++; |
| 163 |
print STDERR "Highest number is now: $highest_number with a count of $highest_count.\n" if ($verbose >= 2); |
| 164 |
} |
| 165 |
} |
| 166 |
return $highest_number; |
| 167 |
} |
| 168 |
|
| 169 |
# This routine will find the median of a set of numbers. |
| 170 |
# If the set of numbers counts to be even, then it will take |
| 171 |
# the higher indexed number in the middle of the set. |
| 172 |
# If the -l option is set, it will take the lower index |
| 173 |
# number in the middle. |
| 174 |
sub find_median { |
| 175 |
my $number_array_ref = shift; |
| 176 |
my @number_array = sort {$a <=> $b} @$number_array_ref; |
| 177 |
my $array_count = scalar(@number_array); |
| 178 |
my $median_index; |
| 179 |
print "The array count is: $array_count\n" if ($verbose >= 2); |
| 180 |
if (($array_count % 2) == 0) { # The index count is even. |
| 181 |
if ($opts{'l'}) { |
| 182 |
$median_index = int($array_count / 2); |
| 183 |
} else { |
| 184 |
$median_index = int(($array_count / 2) + 1); |
| 185 |
} |
| 186 |
} else { |
| 187 |
$median_index = int(($array_count / 2) + 0.5); |
| 188 |
} |
| 189 |
$median_index--; # Translate the number to 0 based array set. |
| 190 |
print "The median index is: " . int(($array_count / 2) - 0.5) . "\n" if ($verbose >= 2); |
| 191 |
my $median = $number_array[$median_index]; |
| 192 |
return $median; |
| 193 |
} |
| 194 |
|
| 195 |
# Lay down some of that perl pod action. |
| 196 |
=pod |
| 197 |
|
| 198 |
=head1 NAME |
| 199 |
|
| 200 |
average - Find the average of a set of numbers. |
| 201 |
|
| 202 |
=head1 SYNOPSIS |
| 203 |
|
| 204 |
B<average> [-dhiIlmMV] <FILE> |
| 205 |
|
| 206 |
| B<average> [-dhiIlmMV] (Input on STDIN from pipeline.) |
| 207 |
|
| 208 |
B<average> [-dhiIlmMV] (Input on STDIN. Use Ctrl-D to stop.) |
| 209 |
|
| 210 |
=head1 DESCRIPTION |
| 211 |
|
| 212 |
B<average> is a program that is part of the numeric utilities package. By default |
| 213 |
B<average> will determine the average from all numbers on input. Other |
| 214 |
options allow you to find the mode and median. |
| 215 |
|
| 216 |
=head1 OPTIONS |
| 217 |
|
| 218 |
-i Only return the integer portion of the final sum. |
| 219 |
-I Only return the decimal portion of the final sum |
| 220 |
|
| 221 |
-m Find the mode (most occuring) of the list of numbers. |
| 222 |
-M Find the median (middle number) of the list of numbers. |
| 223 |
-l When finding the median and the count of numbers in the set is even, |
| 224 |
use the lower middle number instead of the upper middle number. |
| 225 |
|
| 226 |
-h Help: You're looking at it. |
| 227 |
-V Increase verbosity. |
| 228 |
-d Debug mode. For developers |
| 229 |
|
| 230 |
|
| 231 |
=head1 SEE ALSO |
| 232 |
|
| 233 |
bound(1), interval(1), normalize(1), numgrep(1), numprocess(1), numsum(1), random(1), range(1), round(1) |
| 234 |
|
| 235 |
=head1 COPYRIGHT |
| 236 |
|
| 237 |
average is part of the num-utils package, which is copyrighted by |
| 238 |
Suso Banderas and released under the GPL license. Please read |
| 239 |
the COPYING and LICENSE files that came with the num-utils package |
| 240 |
|
| 241 |
Developers can read the GOALS file and contact me about providing |
| 242 |
submitions or help for the project. |
| 243 |
|
| 244 |
=head1 MORE INFO |
| 245 |
|
| 246 |
More info on average can be found at: |
| 247 |
|
| 248 |
=over 1 |
| 249 |
|
| 250 |
=item B<http://suso.suso.org/programs/num-utils/> |
| 251 |
|
| 252 |
=back |
| 253 |
|
| 254 |
=cut |
| 255 |
|