Debian Codegraph @

normalize

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