Debian Codegraph @

numprocess

raw ◀ ▶
Source reconstructed at patch state 3 of 6 — not the installed (fully patched) source.
6 Debian patches touch this file
1 #!/usr/bin/perl -w
2
3 # numprocess: This program mutates numbers as it encounters them.
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);
32
33 getopts('hdV', \%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 my $expression = shift;
53 $expression =~ s/^\/(.*)\/$/$1/;
54
55
56 ################
57 # MAIN PROGRAM #
58 ################
59
60 # For file args
61 if (@ARGV) {
62 foreach $file (@ARGV) {
63 print STDERR "Reading from file $file.\n" if ($verbose >= 2);
64 open (ARGFILE, "$file") && process_filehandle(\*ARGFILE) ||
65 $verbose && warn "Couldn't open file $file for reading: $!\n";
66 close (ARGFILE);
67 }
68 # For STDIN
69 } else {
70 print STDERR "Reading from STDIN.\n" if ($verbose >= 2);
71 process_filehandle(\*STDIN);
72 }
73
74
75 exit(0);
76
77 ###############
78 # SUBROUTINES #
79 ###############
80
81 sub help {
82 print <<"EOF";
83 -----------------------------------------------------------------
84 numprocess: This program mutates numbers as it encounters them.
85 -----------------------------------------------------------------
86 Usage:
87
88 numprocess [options] /<expression>/ <file>
89 | numprocess [options] /<expression>/
90 numprocess [options] /<expression>/
91
92 Options:
93 -d Debug. For developers only.
94 -h Help: You're looking at it.
95 -V Increase verbosity.
96
97 Expressions:
98 Put a list of operations to do on each number in the expression area.
99 Possible expressions:
100
101 + Addition
102 - Subtraction
103 * Multiplication
104 % Division
105 ^ Power function
106 sqrt Square Root
107 sin Sine function
108 cos Cosine function
109
110 Constants and keywords that can be used
111
112 pi 3.141592654
113 e 2.718281828
114
115 See the man page for more details.
116
117 EOF
118 }
119
120 sub process_filehandle {
121 my $filehandle = shift;
122
123 while (<$filehandle>) {
124 if (m/^\s*(\-?[0-9]*\.?[0-9]+)/) {
125 print STDERR "found number: $1\n" if ($verbose >= 3);
126 print process_number($1) . "\n";
127 }
128 }
129 return 1;
130 }
131
132 sub process_number {
133 my $number = shift;
134 my $new_number = $number;
135 my $operation;
136 my $match_expression = "-?[0-9]*\.?[0-9]*";
137
138 foreach $operation (split(/,/, $expression)) {
139 $operation =~ s/pi$/3.141592654/i;
140 $operation =~ s/e$/2.718281828/i;
141
142 if ($operation =~ /^\+($match_expression)$/) {
143 $new_number = $new_number + $1;
144 } elsif ($operation =~ /^\-($match_expression)$/) {
145 $new_number = $new_number - $1;
146 } elsif ($operation =~ /^\*($match_expression)$/) {
147 $new_number = $new_number * $1;
148 } elsif ($operation =~ /^\%($match_expression)$/) {
149 $new_number = $new_number / $1;
150 } elsif ($operation =~ /^\^($match_expression)$/) {
151 $new_number = ($new_number) ** ($1);
152 } elsif ($operation =~ /^sqrt$/) {
153 if ($new_number < 0) { # let's do something nice for negative sqrts.
154 $new_number *= -1;
155 $new_number = sqrt($new_number);
156 $new_number .= "i"; # for imaginary. Just like in High School Algebra.
157 } else {
158 $new_number = sqrt($new_number);
159 }
160 } else {
161 die "Encountered invalid expression $operation in $expression.\n";
162 print "Encountered invalid expression $operation in $expression.\n" if ($verbose >= 2);
163 return $number;
164 }
165 }
166 return $new_number;
167 }
168
169 # Lay down some of that perl pod action.
170 =pod
171
172 =head1 NAME
173
174 numprocess - This program mutates numbers as it encounters them.
175
176 =head1 SYNOPSIS
177
178 B<numprocess> [-dhV] /<expression>/ [FILE or STDIN]
179
180 | B<numprocess> [-dhV] /<expression>/ (Input on STDIN from pipeline.)
181
182 B<numprocess> [-dhV] /<expression>/ (Input on STDIN. Use Ctrl-D to stop.)
183
184 =head1 DESCRIPTION
185
186 B<numprocess>
187 will take as one argument, a list of operations to be performed on numbers that it
188 encounters. It will perform those operations on each number and return the result in place
189 of the original number.
190
191 =head1 USAGE EXAMPLES
192
193 Add 1 to all numbers.
194
195 $ numprocess /+1/ file1
196
197 Convert all numbers from miles to kilometers. Multiply by 8 and divide by 5.
198
199 $ cat file1 | numprocess /*8,%5/
200
201 Convert from celcius to fahreheit degrees. Multiply by 9, divide by 5 and add 32.
202
203 $ numprocess /*9,%5,+32/ temperatures
204
205 Find the area of each circle from the given radius.
206
207 $ numprocess /^2,*pi/ radii
208
209 =head1 KEYWORDS AND OPERATORS
210
211 For operators, the modifying number goes directly after the operator, with
212 the exception of functions like sqrt, sin, cos, etc.
213
214 + Addition
215 - Subtraction
216 * Multiplication
217 % Division
218 ^ Power function
219 sqrt Square Root (*)
220 sin Sine function
221 cos Cosine function
222
223 Constants and keywords that can be used
224
225 pi 3.141592654
226 e 2.718281828
227
228 (*) When using the sqrt operation on negative numbers, it will take the
229 absolute value of the number, sqrt it and then tack an i on the end
230 of the result to signify that the resulting number is imaginary.
231
232 =head1 OPTIONS
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 numaverage(1), numbound(1), numinterval(1), numnormalize(1), numgrep(1), numsum(1), numrandom(1), numrange(1), numround(1)
242
243 =head1 BUGS
244
245 There is currently no way to take the number found in the text stream and
246 use it as the numerator instead of the denominator of a division operation.
247
248
249 =head1 COPYRIGHT
250
251 numprocess is part of the num-utils package, which is copyrighted by
252 Suso Banderas and released under the GPL license. Please read
253 the COPYING and LICENSE files that came with the num-utils package
254
255 Developers can read the GOALS file and contact me about providing
256 submitions or help for the project.
257
258 =head1 MORE INFO
259
260 More info on numprocess can be found at:
261
262 =over 1
263
264 =item B<http://suso.suso.org/programs/num-utils/>
265
266 =back
267
268 =cut
269