Debian Codegraph @

normalize

raw ◀ ▶
3 Debian patches touch this file
1 #!/usr/bin/perl -w
2
•3 # numnormalize: Normalize a set of numbers. By default between 0 and 1.
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 my ($file, @number_array, @output_array, $number);
33
34 getopts('hdR:V', \%opts);
35
36 if ($opts{'h'}) {
37 &help();
38 exit(0);
39 }
40
41 if ($opts{'d'}) {
42 $verbose = 3;
43 print STDERR "Debug mode\n";
44 } elsif ($opts{'V'}) {
45 $verbose = 2;
46 print STDERR "Verbose mode\n";
47 } elsif ($opts{'q'}) {
48 $verbose = 0; # Nothing except the final answer
49 } else {
50 $verbose = 1; # Normal warnings and such.
51 }
52
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 @output_array = normalize(\@number_array);
74
75 foreach $number (@output_array) {
76 print "$number\n";
77 }
78
79 exit(0);
80
81 ###############
82 # SUBROUTINES #
83 ###############
84
85 sub help {
86 print <<"EOF";
87 ----------------------------------------------
•88 numnormalize : Normalize a set of numbers.
89 ----------------------------------------------
90 Usage:
91
•92 numnormalize [options] <file>
93 | numnormalize [options]
94 numnormalize [options]
95
96 Options:
97 -d Debug. For developers only.
98 -h Help: You're looking at it.
99 -V Increase verbosity.
100
101 -R <range> This allows you to specify a different
102 normalized range instead of from 0 to 1.
103 For example: -R 0..5
104 EOF
105 }
106
•107 sub HELP_MESSAGE{
108 &help();
109 }
110
111 sub process_filehandle {
112 my $filehandle = shift;
113 my $number_array_ref = shift;
114
115 while (<$filehandle>) {
116 if (m/^\s*(\-?[0-9]*\.?[0-9]+)/) {
117 print STDERR "found number: $1\n" if ($verbose >= 3);
118 push(@$number_array_ref, $1);
119 }
120 }
121 return 1;
122 }
123
124 sub normalize {
125 my $number_array_ref = shift;
126 my ($sum, @normalized_array);
127 my ($top_range, $bottom_range);
128 if ($opts{'R'}) {
129 ($bottom_range,$top_range) = split(/\.\./, $opts{'R'});
130 print "R: $opts{'R'}\nbottom: $bottom_range\ntop: $top_range\n" if ($verbose >= 2);
131 } else {
132 $bottom_range = 0;
133 $top_range = 1;
134 }
135
136 foreach $number (@$number_array_ref) {
137 $sum += $number;
138 }
139 foreach $number (@$number_array_ref) {
140 my $normalized_number = ($number / $sum) * ($top_range - $bottom_range) + $bottom_range;
141 push(@normalized_array, $normalized_number);
142 }
143 return @normalized_array;
144 }
145
146 # Lay down some of that perl pod action.
147 =pod
148
149 =head1 NAME
150
•151 numnormalize - Normalize a set of numbers. By default between 0 and 1.
152
153 =head1 SYNOPSIS
154
•155 B<numnormalize> [-dhRV] <FILE>
156
•157 | B<numnormalize> [-dhRV] (Input on STDIN from pipeline.)
158
•159 B<numnormalize> [-dhRV] (Input on STDIN. Use Ctrl-D to stop.)
160
161 =head1 DESCRIPTION
162
•163 B<numnormalize>
164 will take a set of numbers on input and return that set as a normalized set of numbers
165 between 0 and 1 by default. Or you can use the -R option to specify a different normalized
166 range.
167
168 =head1 OPTIONS
169
170 -h Help: You're looking at it.
171 -V Increase verbosity.
172 -d Debug mode. For developers
173
174 -R <range> This allows you to specify a different normalized range instead of from 0 to 1.
175 For example -R 0..5
176
177
178 =head1 SEE ALSO
179
•180 numaverage(1), numbound(1), numinterval(1), numgrep(1), numprocess(1), numsum(1), numrandom(1), numrange(1), numround(1)
181
182 =head1 COPYRIGHT
183
•184 numnormalize is part of the num-utils package, which is copyrighted by
185 Suso Banderas and released under the GPL license. Please read
186 the COPYING and LICENSE files that came with the num-utils package
187
188 Developers can read the GOALS file and contact me about providing
189 submitions or help for the project.
190
191 =head1 MORE INFO
192
•193 More info on numnormalize can be found at:
194
195 =over 1
196
•197 =item B<http://suso.suso.org/xulu/Num-utils>
198
199 =back
200
201 =cut
202