Debian Codegraph @

numprocess

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