Debian Codegraph @

numprocess

raw ◀ ▶
Source reconstructed at patch state 1 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 print "Encountered invalid expression $operation in $expression.\n" if ($verbose >= 2);
162 return $number;
163 }
164 }
165 return $new_number;
166 }
167
168 # Lay down some of that perl pod action.
169 =pod
170
171 =head1 NAME
172
173 numprocess - This program mutates numbers as it encounters them.
174
175 =head1 SYNOPSIS
176
177 B<numprocess> [-dhV] /<expression>/ [FILE or STDIN]
178
179 | B<numprocess> [-dhV] /<expression>/ (Input on STDIN from pipeline.)
180
181 B<numprocess> [-dhV] /<expression>/ (Input on STDIN. Use Ctrl-D to stop.)
182
183 =head1 DESCRIPTION
184
185 B<numprocess>
186 will take as one argument, a list of operations to be performed on numbers that it
187 encounters. It will perform those operations on each number and return the result in place
188 of the original number.
189
190 =head1 USAGE EXAMPLES
191
192 Add 1 to all numbers.
193
194 $ numprocess /+1/ file1
195
196 Convert all numbers from miles to kilometers. Multiply by 8 and divide by 5.
197
198 $ cat file1 | numprocess /*8,%5/
199
200 Convert from celcius to fahreheit degrees. Multiply by 9, divide by 5 and add 32.
201
202 $ numprocess /*9,%5,+32/ temperatures
203
204 Find the area of each circle from the given radius.
205
206 $ numprocess /^2,*pi/ radii
207
208 =head1 KEYWORDS AND OPERATORS
209
210 For operators, the modifying number goes directly after the operator, with
211 the exception of functions like sqrt, sin, cos, etc.
212
213 + Addition
214 - Subtraction
215 * Multiplication
216 % Division
217 ^ Power function
218 sqrt Square Root (*)
219 sin Sine function
220 cos Cosine function
221
222 Constants and keywords that can be used
223
224 pi 3.141592654
225 e 2.718281828
226
227 (*) When using the sqrt operation on negative numbers, it will take the
228 absolute value of the number, sqrt it and then tack an i on the end
229 of the result to signify that the resulting number is imaginary.
230
231 =head1 OPTIONS
232
233 -h Help: You're looking at it.
234 -V Increase verbosity.
235 -d Debug mode. For developers
236
237
238 =head1 SEE ALSO
239
240 numaverage(1), numbound(1), numinterval(1), numnormalize(1), numgrep(1), numsum(1), numrandom(1), numrange(1), numround(1)
241
242 =head1 BUGS
243
244 There is currently no way to take the number found in the text stream and
245 use it as the numerator instead of the denominator of a division operation.
246
247
248 =head1 COPYRIGHT
249
250 numprocess 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 numprocess 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